我需要访问类值的帮助。
这是我的Node类。
class Node():
def __init__(self, x, y):
self.x = x
self.y = y
self.parent = None这是我访问上面的类的另一个类。
class RRT():
def __init__(self, start, goal, obstacle_list):
self.start = Node(start[0], start[1]) # start node for the RRT
self.goal = Node(goal[0], goal[1]) # goal node for the RRT
self.obstacle_list = obstacle_list # list of obstacles
self.node_list = [] # list of nodes added while creating the RRT
...我将start存储在我的RRT类中的另一个函数中,如下所示的self.node_list = [self.start]
def getNearestNode(self, random_point):
minDist = 1e5
for ii in self.node_list:
nodePt = [None, None]
# error occurs at line below
nodePt[0] = self.node_list[ii].x
nodePt[1] = self.node_list[ii].y
# this function takes 2 coordinate lists and computes the distance
dist = self.calcDistNodeToPoint(nodePt, random_point)
if dist < minDist:
minDist = dist
index = ii
return index我收到此错误消息TypeError: list indices must be integers or slices, not Node
发布于 2019-02-24 03:39:54
Python for循环是for-each循环。ii是元素本身,而不是索引。
您应该执行以下操作:
nodePt[0] = ii.x
nodePt[1] = ii.y或者更好的做法是,在此之前不要定义nodePt,只需定义:
nodePt = ii.x, ii.yhttps://stackoverflow.com/questions/54845427
复制相似问题