我试图建立一个二维数组,其值对应于它们各自的索引。但由于某种原因,外环似乎与内环很好地啮合。以下是简化代码及其各自的输出。
>>> cells = [[None]*3]*3
>>> for x in range(3):
... for y in range(3):
... print((x,y))
... cells[x][y] = (x,y)
...
(0, 0)
(0, 1)
(0, 2)
(1, 0)
(1, 1)
(1, 2)
(2, 0)
(2, 1)
(2, 2)
好的,所以在循环中打印元组表明x值正在递增。但在检查数组时......
>>> for c in cells:
... print(c)
...
[(2, 0), (2, 1), (2, 2)]
[(2, 0), (2, 1), (2, 2)]
[(2, 0), (2, 1), (2, 2)]
有人能解释一下这里发生了什么吗?