Skip to main content
 首页 » 编程设计

当我只更改一个时,Python 填充二维列表(或数组)的所有元素

2025年05月04日14birdshome

<分区>

Possible Duplicate:
Python : how to append new elements in a list of list?

我在 Python 中想到了这种(对我来说)非常奇怪的行为。

我有一个空的二维列表(如果您愿意,也可以是数组),当我向其中一列添加元素时,所有其他列都会获得相同的附加值。

代码如下:

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, 2, 3, 4], [1, 2, 3, 4]]