可能重复:
Unexpected feature in a Python list of lists环路改变矩阵值给出了奇怪的结果
我有11充满0的9矩阵我要让每一个的第一要素行,每列的第一个元素的分数比前一分小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中的所有行?
@senderle - 好的发现。这是一个很常见的问题,但标题遍布整个地方,当我看到它出现时很难追查一个副本。 – mgilson
@mgilson,这确实很难。在这种情况下,所有功劳都应该到[flebool](http://stackoverflow.com/a/13176124/577088)! – senderle