在循环中改变矩阵值会产生奇怪的结果

时间:2012-11-01 11:24:29

标签: python matrix

  

可能重复:
  Unexpected feature in a Python list of lists

我有一个填充了0到9的11的矩阵。我想制作每一行的第一个元素,每列的第一个元素的分数比前一个小-2,所以:

[[0, -2, -4], [-2, 0, 0], [-4, 0, 0]]

为此,我使用以下代码:

# make a matrix of length seq1_len by seq2_len filled with 0's\
x_row = [0]*(seq1_len+1)
matrix = [x_row]*(seq2_len+1)
# all 0's is okay for local and semi-local, but global needs 0, -2, -4 etc at first elmenents
# because of number problems need to make a new matrix
if align_type == 'global':
    for column in enumerate(matrix):
        print column[0]*-2
        matrix[column[0]][0] = column[0]*-2
for i in matrix:
    print i

结果:

0
-2
-4
-6
-8
-10
-12
-14
-16
[-16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[-16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[-16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[-16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[-16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[-16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[-16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[-16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[-16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

为什么它将列[0] * - 2的最后一个值赋予所有行?

2 个答案:

答案 0 :(得分:3)

尝试运行此代码:

x_row = [0]*(seq1_len+1)

matrix = [x_row]*(seq2_len+1)

matrix[4][5]=5

print matrix

你知道5已经扩散了。发生这种情况是因为第二行代码复制了相同的数据。看到这个

List of lists changes reflected across sublists unexpectedly

答案 1 :(得分:1)

这是因为您创建列表列表的方式会产生一个列表,其中包含相同的列表对象,即相同的id(),更改一个实际上也会更改其他列表对象:< / p>

In [4]: x_row = [0]*(5+1)

In [5]: matrix = [x_row]*(7+1)

In [6]: [id(x) for x in matrix]
Out[6]:                         #same id()'s, means all are same object
[172797804,
 172797804,
 172797804,
 172797804,
 172797804,
 172797804,
 172797804,
 172797804]

In [20]: matrix[0][1]=5 #changed only one

In [21]: matrix         #but it changed all
Out[21]: 
[[0, 5, 0, 0, 0, 0],
 [0, 5, 0, 0, 0, 0],
 [0, 5, 0, 0, 0, 0],
 [0, 5, 0, 0, 0, 0],
 [0, 5, 0, 0, 0, 0],
 [0, 5, 0, 0, 0, 0],
 [0, 5, 0, 0, 0, 0],
 [0, 5, 0, 0, 0, 0]]

你应该以这种方式创建矩阵以避免这种情况:

In [12]: matrix=[[0]*6 for _ in range(9)]

In [13]: [id(x) for x in matrix]
Out[13]:                           #different id()'s, so different objects 
[172796428,              
 172796812,
 172796364,
 172796268,
 172796204,
 172796140,
 172796076,
 172795980,
 172795916]

In [23]: matrix[0][1]=5  #changed only one

In [24]: matrix         #results as expected
Out[24]: 
[[0, 5, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0]]