Python array -- elements replaced with last value -



Python array -- elements replaced with last value -

given code

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

the output of sumset([[1,2], [2,4]], [[0,1], [8, 9]]) gives

[1, 3] [9, 11] [2, 5] [10, 13] [[10, 13], [10, 13], [10, 13], [10, 13]]

i cannot understand why bands filled bands[3].

edit: using python 3.4.2 on ubuntu 14.10.

by multiplying list you're creating copies of initial list. when modify 1 of them, changes transfered other copies well.

python

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -