当pole[i][j] == 1
时我需要采取行动,但它会抛出错误:
TypeError: list indices must be integers or slices, not list
pole = [[0,1,0],
[1,0,1],
[0,1,1]]
for i in pole:
for j in pole:
if pole[i][j] == 1:
my_code()
最佳答案
您使用 for i in pole
获取 pole
的实际值而不是索引。这是更正后的代码:
pole = [[0,1,0],
[1,0,1],
[0,1,1]]
for i in range(len(pole)):
for j in range(len(pole[i])):
if pole[i][j] == 1:
my_code()
或者,您可以使用值而不是索引:
pole = [[0,1,0],
[1,0,1],
[0,1,1]]
for i in pole:
for j in i:
if j == 1:
my_code()
关于python - 列表索引必须不是列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70382639/