List Index for lists that create a grid
Question:
I’m trying to find the index of an element in list, obviously I tried the normal list.index() method but its says that the item is not in the list (but it is). Since I’m creating a grid with this list, maybe there is an other method.
Here is the code.
grid = [[200, 200, 200, 200, 200, 200, 200], [200, 200, 200, 400, 200, 78, 200], [200, 200, 400, 400, 200, 75, 200], [200, 200, 400, 400, 300, 200, 200], [200, 300, 200, 400, 400, 200, 200], [200, 14, 200, 52, 200, 84, 200], [200, 200, 200, 200, 200, 200, 200]]
print(grid.index(200))
The error is:
ValueError: 200 is not in list
But I can see that grid[0][0] is 200
Hear a photo of the grid created from the list enter image description here
Answers:
Grid is a list of lists. You need to instead index each list inside the "outer" list. For example:
for l in grid:
print(l.index(200))
You search for an item as cell, but you search in the outter list, rows list
def get_row_col(grid, num):
for i, row in enumerate(grid):
if num in row:
return (i, row.index(num))
return None
grid = [[200, 200, 200, 200, 200, 200, 200], [200, 200, 200, 400, 200, 78, 200], [200, 200, 400, 400, 200, 75, 200], [200, 200, 400, 400, 300, 200, 200], [200, 300, 200, 400, 400, 200, 200], [200, 14, 200, 52, 200, 84, 200], [200, 200, 200, 200, 200, 200, 200]]
get_row_col(grid, 52)
(1, 5)
You could use the following to get a List of Tuples containing the two co-ordinates in your List of Lists:
seeking = 200
idx = [(i,j) for i, x in enumerate(grid) for j, y in enumerate(x) if y == seeking]
print(idx)
which produces
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (1, 0), (1, 1), (1, 2), (1, 4), (1, 6), (2, 0), (2, 1), (2, 4), (2, 6), (3, 0), (3, 1), (3, 5), (3, 6), (4, 0), (4, 2), (4, 5), (4, 6), (5, 0), (5, 2), (5, 4), (5, 6), (6, 0), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6)]
so (0,0) means subList 0, element 0 and so on
I’m trying to find the index of an element in list, obviously I tried the normal list.index() method but its says that the item is not in the list (but it is). Since I’m creating a grid with this list, maybe there is an other method.
Here is the code.
grid = [[200, 200, 200, 200, 200, 200, 200], [200, 200, 200, 400, 200, 78, 200], [200, 200, 400, 400, 200, 75, 200], [200, 200, 400, 400, 300, 200, 200], [200, 300, 200, 400, 400, 200, 200], [200, 14, 200, 52, 200, 84, 200], [200, 200, 200, 200, 200, 200, 200]]
print(grid.index(200))
The error is:
ValueError: 200 is not in list
But I can see that grid[0][0] is 200
Hear a photo of the grid created from the list enter image description here
Grid is a list of lists. You need to instead index each list inside the "outer" list. For example:
for l in grid:
print(l.index(200))
You search for an item as cell, but you search in the outter list, rows list
def get_row_col(grid, num):
for i, row in enumerate(grid):
if num in row:
return (i, row.index(num))
return None
grid = [[200, 200, 200, 200, 200, 200, 200], [200, 200, 200, 400, 200, 78, 200], [200, 200, 400, 400, 200, 75, 200], [200, 200, 400, 400, 300, 200, 200], [200, 300, 200, 400, 400, 200, 200], [200, 14, 200, 52, 200, 84, 200], [200, 200, 200, 200, 200, 200, 200]]
get_row_col(grid, 52)
(1, 5)
You could use the following to get a List of Tuples containing the two co-ordinates in your List of Lists:
seeking = 200
idx = [(i,j) for i, x in enumerate(grid) for j, y in enumerate(x) if y == seeking]
print(idx)
which produces
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (1, 0), (1, 1), (1, 2), (1, 4), (1, 6), (2, 0), (2, 1), (2, 4), (2, 6), (3, 0), (3, 1), (3, 5), (3, 6), (4, 0), (4, 2), (4, 5), (4, 6), (5, 0), (5, 2), (5, 4), (5, 6), (6, 0), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6)]
so (0,0) means subList 0, element 0 and so on