Python数组--用最后一个值替换元素

Python数组--用最后一个值替换元素,python,Python,给定代码 def sumset(a, b): bands=[[0, 0]]*len(a)*len(b) current=-1 for ba in a: for bb in b: current+=1 bands[current][0]=ba[0]+bb[0] bands[current][1]=ba[1]+bb[1] print(bands[current]) print(bands

给定代码

  def sumset(a, b):
    bands=[[0, 0]]*len(a)*len(b)
    current=-1
    for ba in a:
      for bb in b:
        current+=1
        bands[current][0]=ba[0]+bb[0]
        bands[current][1]=ba[1]+bb[1]
        print(bands[current])

    print(bands)
sumset([[1,2],[2,4],[[0,1],[8,9]])
的输出给出

[1, 3]
[9, 11]
[2, 5]
[10, 13]
[[10, 13], [10, 13], [10, 13], [10, 13]]
我不明白为什么
波段
中充满了
波段[3]


编辑:我在Ubuntu 14.10上使用Python 3.4.2。

通过乘以列表,您将创建初始列表的副本。因此,当您修改其中一个副本时,更改也会转移到其他副本中。

使用范围(len(a)*len(b))中i的波段=[[0,0]来避免它。检查下面的答案