我有一个列表“地图”,我想用作用于初始数组的每个位置的函数“counterPosition”的相应输出值替换所有元素,如下所示:
map[0][0] = counterPosition(0,0)
map[0][1] = counterPosition(0,1)
...
像这样一个一个地做我可以得到答案,但是当我尝试这样的事情时:
for x in range (len(map)):
for y in range (len(map)):
map[x][y] = counterPosition(x,y)
它不起作用......我做错了什么吗?
编辑:
def counterPosition(x, y):
bombs = 0
for i in range(x-1, x+2):
for j in range(y-1, y+2):
if i<0 or j<0: continue
elif map[i][j] == True:
bombs += 1
return bombs
map = [[True, False, False, False, False, True],
[False, False, False, False, False, True],
[True, True, False, True, False, True],
[True, False, False, False, False, False],
[False, False, True, False, False, False],
[False, False, False, False, False, False]]
错误是:
IndexError: list index out of range