Nested Lists Assignment Problem

tiksin tiksin at free.fr
Wed Apr 26 04:38:30 EDT 2006


Le Mercredi 26 Avril 2006 10:13, Licheng Fang a écrit :
> I wanna use nested lists as an array, but here's the problem:
> >>> a = [[0]*3]*3
> >>> a
>
> [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
>
> >>> a[0][0] = 1
> >>> a
>
> [[1, 0, 0], [1, 0, 0], [1, 0, 0]]
>
> Could anybody please explain to me why three values were change? I'm
> bewildered. Thanks!

I guess is a probleme of reference. Write that instead:

>>>l = [ [0]*3 for i in xrange(3) ]

cordialement,



More information about the Python-list mailing list