mat*_*ttm 2 python iterable class hill-climbing
在我的功能中,我有:
"""
Iterates 300 times as attempts, each having an inner-loop
to calculate the z of a neighboring point and returns the optimal
"""
pointList = []
max_p = None
for attempts in range(300):
neighborList = ( (x - d, y), (x + d, y), (x, y - d), (x, y + d) )
for neighbor in neighborList:
z = evaluate( neighbor[0], neighbor[1] )
point = None
point = Point3D( neighbor[0], neighbor[1], z)
pointList += point
max_p = maxPoint( pointList )
x = max_p.x_val
y = max_p.y_val
return max_p
Run Code Online (Sandbox Code Playgroud)
我没有迭代我的类实例,指出,但我仍然得到:
pointList += newPoint
TypeError: 'Point3D' object is not iterable
Run Code Online (Sandbox Code Playgroud)
问题是这一行:
pointList += point
Run Code Online (Sandbox Code Playgroud)
pointList
是一个list
并且point
是一个Point3D
实例.您只能将另一个iterable添加到iterable中.
你可以解决这个问题:
pointList += [point]
Run Code Online (Sandbox Code Playgroud)
要么
pointList.append(point)
Run Code Online (Sandbox Code Playgroud)
在你的情况并不需要分配None
给point
.您也不需要将变量绑定到新点.您可以将它直接添加到列表中,如下所示:
pointList.append(Point3D( neighbor[0], neighbor[1], z))
Run Code Online (Sandbox Code Playgroud)