This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author beco
Recipients beco
Date 2007-11-08.21:14:07
SpamBayes Score 0.0013582347
Marked as misclassified No
Message-id <1194556448.46.0.361178031536.issue1408@psf.upfronthosting.co.za>
In-reply-to
Content
There is no way to create a big nested list without references using the
multiplication operator.

'*' is supposed to work like + ... + in this cases:

>>> a=[0, 0]
>>> b=[a[:]]+[a[:]]
>>> b
[[0, 0], [0, 0]]
>>> b[0][0]=1
>>> b
[[1, 0], [0, 0]]

Ok! Copy here, not reference. Mainly because we use [:] explicitly
expressing we want a copy.

>>> c=[a[:]]*2
>>> c
[[0, 0], [0, 0]]
>>> c[0][0]=2
>>> c
[[2, 0], [2, 0]]

Inconsistence here. It is supposed to be clear and copy, not reference
in between.

Consequence: there is no clear way to create a nested list of, lets say,
60x60, using multiplications.

Even when using this, we cannot deal with the problem:

>>> import copy
>>> d=[copy.deepcopy(a[:])]*2
>>> d
[[0, 0], [0, 0]]
>>> d[0][0]=3
>>> d
[[3, 0], [3, 0]]

Workaround:
>>> from numpy import *
>>> a=zeros((2,2),int)
>>> a
array([[0, 0],
       [0, 0]])
>>> b=a.tolist()
>>> b
[[0, 0], [0, 0]]
>>> b[0][0]=4
>>> b 
[[4, 0], [0, 0]]

And that is the expected behaviour.

Thanks.
History
Date User Action Args
2007-11-08 21:14:08becosetspambayes_score: 0.00135823 -> 0.0013582347
recipients: + beco
2007-11-08 21:14:08becosetspambayes_score: 0.00135823 -> 0.00135823
messageid: <1194556448.46.0.361178031536.issue1408@psf.upfronthosting.co.za>
2007-11-08 21:14:08becolinkissue1408 messages
2007-11-08 21:14:07becocreate