可能重复:
Python : how to append new elements in a list of list?Python中填充一个二维表(或阵列)的所有元素,当我改变只是一个
我想出了这个奇怪的(对我来说)在Python中的行为。
我有一个空的2D列表(或者你喜欢的数组),当我向其中一列添加一个元素时,所有其他列都会得到相同的值。
下面是代码:
row = [1, 2, 3, 4]
yChannel = 4*[[]]
sectorPlace = 0
for sector in yChannel:
sector.append(row[sectorPlace])
sectorPlace += 1
print yChannel
和输出:
[[1,2,3,4],[1,2,3,4],[1, 1,2,3,4],[1,2,3,4]]
这是python中最常见的疑问之一:请参阅 –