我知道有类似于这个here,here,和here的问题。第一个是1D列表,第二个是很棒的,除了它似乎不工作,第三个是接近,但我仍然不太明白我的问题。
这就是我想要做的。我需要创建一个2D列表( java和C++中的2D数组,我更熟悉),它需要大小为20,向下15。
以下是我尝试过的:
self.grid = [[0 for x in range(GRID_COLUMN_SIZE)] for y in range(GRID_ROW_SIZE)] # where GRID_ROW_SIZE = 15, GRID_COLUMN_SIZE = 20注意,我尝试过切换两个常量(首先是COLUMN,然后是ROW),然后稍微中断。此外,我打印出2D列表,这是错误的维度(15跨,20向下)。
下面是我以后对self.grid的使用情况。在不太深入的情况下,我将遍历列表的所有值(grid)并获取周围的点。
def populatePaths(self):
for row in range(len(self.grid)):
for column in range(len(self.grid[row])):
if self.isPointAccessible(column, row):
self.addPaths(column, row)
def addPaths(self, x, y):
key = Point(x, y)
print "Each: %s" % (key.toString())
points = key.getSurroundingPoints()
self.removeBarriersFromPath(points)
self.paths[key] = points # a map from Points to lists of surrounding Points基本上,我删除了无法到达的路径上的点:
def removeBarriersFromPath(self, path):
for point in list(path):
print "Surrounding %s" % (point.toString())
if not self.isPointAccessible(point.x, point.y):
path.remove(point)
return pathself.isPointAccessible()是微不足道的,但它就是在这里崩溃的。它检查(x,y)位置上的值是否为0:return self.grid[x][y] == 0
我添加了这些print语句(point.toString()返回(x,y)),以便在它们发生时向我显示点,并且我能够迭代直到x==14,但它在x==15处中断。
我怀疑我得到的列/行顺序循环不正确,但我不确定何时/如何。
如果我解释得不够清楚就告诉我。
编辑这里是回溯:
Traceback (most recent call last):
File "/home/nu/catkin_ws/src/apriltags_intrude_detector/scripts/sphero_intrude_gui.py", line 70, in start
self.populatePaths()
File "/home/nu/catkin_ws/src/apriltags_intrude_detector/scripts/sphero_intrude_gui.py", line 156, in populatePaths
self.addPaths(column, row)
File "/home/nu/catkin_ws/src/apriltags_intrude_detector/scripts/sphero_intrude_gui.py", line 162, in addPaths
self.removeBarriersFromPath(points)
File "/home/nu/catkin_ws/src/apriltags_intrude_detector/scripts/sphero_intrude_gui.py", line 168, in removeBarriersFromPath
if not self.isPointAccessible(point.x, point.y):
File "/home/nu/catkin_ws/src/apriltags_intrude_detector/scripts/sphero_intrude_gui.py", line 173, in isPointAccessible
return self.grid[x][y] == 0
IndexError: list index out of range发布于 2016-05-10 05:00:24
您没有发布isPointAccessible的全部源代码,但从错误消息看,您的返回行必须如下:
return self.grid[y][x] == 0因为y表示行号,而x是列。
https://stackoverflow.com/questions/37129440
复制相似问题