Cache Staheli Cache Staheli - 2 years ago 75
Python Question

List index out of range in 2D list

I understand that there are similar questions to this here, here, and here. The first one addresses 1D lists, the second is great except it doesn't seem to work, and the third is close, but I still don't quite understand my issue.

Here is what I am trying to do. I need to create an 2D list (a 2D array in java and C++, which I am much more familiar with) filled with 0s. It needs to be size
20 across and 15 down.

Here is what I have tried:

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

Note, I tried with the two constants switched (
first, then
), and it broke slightly later. In addition, I print the 2D list out and it's the wrong dimensions (15 across and 20 down).

Here is my later use of
. Without getting two deep, I am iterating through all the values of the list(
) and getting the surrounding points.

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.paths[key] = points # a map from Points to lists of surrounding Points

Basically, I remove points along the path that can't be reached:

def removeBarriersFromPath(self, path):
for point in list(path):
print "Surrounding %s" % (point.toString())
if not self.isPointAccessible(point.x, point.y):
return path

is trivial, but this is where it breaks. It checks to see if the value at the
location is 0:
return self.grid[x][y] == 0

I added these print statements (
) to show me the points as they happen, and I am able to iterate until
, but it breaks at

I suspect that I am getting the column/row order in the looping incorrect, but I'm not sure when/how.

Let me know if I didn't explain something clearly enough.

Edit Here is the traceback:

Traceback (most recent call last):
File "/home/nu/catkin_ws/src/apriltags_intrude_detector/scripts/", line 70, in start
File "/home/nu/catkin_ws/src/apriltags_intrude_detector/scripts/", line 156, in populatePaths
self.addPaths(column, row)
File "/home/nu/catkin_ws/src/apriltags_intrude_detector/scripts/", line 162, in addPaths
File "/home/nu/catkin_ws/src/apriltags_intrude_detector/scripts/", line 168, in removeBarriersFromPath
if not self.isPointAccessible(point.x, point.y):
File "/home/nu/catkin_ws/src/apriltags_intrude_detector/scripts/", line 173, in isPointAccessible
return self.grid[x][y] == 0
IndexError: list index out of range

Answer Source

You did not post the whole source for isPointAccessible but from the error message it looks like your return line must be:

return self.grid[y][x] == 0

since y denotes the row number and x is the column.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download