我正在尝试创建预期结果为
的2D图[[(-1, 1), (0,1), (1,1)],
[(-1, 0), (0,0), (1,0)],
[(-1,-1), (0,-1), (1,-1)]]
带有我的代码:
SIZE = 3
chessBoard = SIZE * [SIZE * [None]]
XY = [i for i in range(-int(SIZE/2), int(SIZE/2) + 1)]
for i in range(SIZE):
for j in range(SIZE):
chessBoard[i][j] = (XY[j], XY[::-1][i])
print(chessBoard)`
但是该函数的结果是:
[(-1, -1), (0, -1), (1, -1)],
[(-1, -1), (0, -1), (1, -1)],
[(-1, -1), (0, -1), (1, -1)]]
元组中的第二个值是恒定的。我怎么了 如果有帮助的话,就好像它为每个for循环修改了整个列。仍然不清楚为什么会发生这种情况。