基本数组标识问题

array1=[[0 for rows in range(9)] for cols in range(9)]
array2=list(array1)
array1[0][0]=1
print(array2[0][0])
print(id(array1) is id(array2))

这给出: 1个 假

数组具有单独的ID,但是,更改array1仍会更改array2。为什么?

评论
nin
nin

因为您要更改的子数组不是副本。

print(id(array1[0]), id(array2[0]))

Also, don't use is for comparing... practically anything but None, True and False.

点赞
评论