Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #29862

Re: List Problem

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <d@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'broken': 0.03; 'debug': 0.05; 'item.': 0.07; 'nested': 0.07; 'python': 0.09; '[1,': 0.09; 'other,': 0.09; 'cc:addr:python-list': 0.10; 'essential': 0.15; '3],': 0.16; 'alist': 0.16; 'subject:Problem': 0.16; 'those,': 0.16; 'wrote:': 0.17; 'code,': 0.18; 'code.': 0.20; 'changes': 0.20; 'cc:2**0': 0.23; 'work.': 0.23; 'references': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In- Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'language.': 0.27; 'trouble': 0.28; 'post': 0.28; 'no,': 0.29; 'objects': 0.29; 'probably': 0.29; 'print': 0.32; 'cases,': 0.33; 'subject:List': 0.33; 'code:': 0.33; 'skip:d 20': 0.34; 'list': 0.35; 'so,': 0.35; 'pm,': 0.35; 'posting': 0.35; 'list.': 0.35; 'but': 0.36; 'too': 0.36; 'usual': 0.37; 'subject:: ': 0.38; 'fact': 0.38; 'sure': 0.38; 'received:192': 0.39; 'list,': 0.39; 'where': 0.40; 'received:192.168': 0.40; 'link': 0.60; 'skip:n 10': 0.63; 'more': 0.63; 'header:Reply-To:1': 0.68; 'received:74.208': 0.71; 'reply- to:no real name:2**0': 0.72; 'received:74.208.4.194': 0.84; 'understand,': 0.84
Date Sun, 23 Sep 2012 18:03:32 -0400
From Dave Angel <d@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120714 Thunderbird/14.0
MIME-Version 1.0
To jimbo1qaz <jimmyli1528@gmail.com>
Subject Re: List Problem
References <5126348a-8e87-493d-975c-d6273e59784c@googlegroups.com> <b6555e91-54ba-42fb-bd99-561cb971c2ce@googlegroups.com>
In-Reply-To <b6555e91-54ba-42fb-bd99-561cb971c2ce@googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:XtZ0YHQc70gjfqR1aEZsNKvDTQtlk93qOB8+EaAtvJC ViKjBKfN0rAMo+UO0oJD0AAnvR4ynr52Fc+t6CLWLG1V7xoevD W9gQY3wAVeBWKgvN48+VpGIL+NxpY1m/AOLzY5S39AjOxTTkHw J9lwJZqfBmSMmD8zdYkbNeoiITaq12als1tMNc1LD/GUeOtPv/ vQvjU2SESDGu3jLN4nm5YlIjpEztN2Z0W0artSXMbmC2rUxHTf OYmqo65YRvW0wSZgSLhyewMtYyckymJbkOX7yoJH0dw9Vrrg3d ShO+F59x7xi2/LtlZb3LNEq345mbtWQu1L0KVxqarEicQOgGA= =
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
Reply-To d@davea.name
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1158.1348437838.27098.python-list@python.org> (permalink)
Lines 41
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1348437838 news.xs4all.nl 6912 [2001:888:2000:d::a6]:54547
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:29862

Show key headers only | View raw


On 09/23/2012 05:44 PM, jimbo1qaz wrote:
> On Sunday, September 23, 2012 2:31:48 PM UTC-7, jimbo1qaz wrote:
>> I have a nested list. Whenever I make a copy of the list, changes in one affect the other, even when I use list(orig) or even copy the sublists one by one. I have to manually copy each cell over for it to work.
>>
>> Link to broken code: http://jimbopy.pastebay.net/1090401
> No, actually that's the OK code. http://jimbopy.pastebay.net/1090494 is the broken one.

I also would prefer an inline posting of the code, but if it's too big
to post here, it's probably too big for me to debug here.

The usual reason for such a symptom is a nested list, where you have
multiple references to the same inner list inside the outer.  When you
change one of those, you change all of them.

alist = [1, 2, 3]
blist = [alist, alist, alist]   #  or blist = alist * 3
print blist
alist.append(49)
print blist

davea@think:~/temppython$ python jimbo.py
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]
[[1, 2, 3, 49], [1, 2, 3, 49], [1, 2, 3, 49]]

Solution to this is to make sure that only copies of alist get into
blist.  One way is

blist = [alist[:], alist[:], alist[:]]

More generally, you can get into this type of trouble whenever you have
non-immutable objects inside the list.

Understand, this is NOT a flaw in the language.  It's perfectly
reasonable to be able to do so, in fact essential in many cases, when
you want it to be the SAME item.


-- 

DaveA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

List Problem jimbo1qaz <jimmyli1528@gmail.com> - 2012-09-23 14:31 -0700
  Re: List Problem jimbo1qaz <jimmyli1528@gmail.com> - 2012-09-23 14:44 -0700
    Re: List Problem Chris Angelico <rosuav@gmail.com> - 2012-09-24 07:57 +1000
    Re: List Problem Dave Angel <d@davea.name> - 2012-09-23 18:03 -0400
    Re: List Problem Chris Angelico <rosuav@gmail.com> - 2012-09-24 08:27 +1000
      Re: List Problem Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-23 22:37 +0000
        Re: List Problem Chris Angelico <rosuav@gmail.com> - 2012-09-24 08:45 +1000
    Re: List Problem Chris Angelico <rosuav@gmail.com> - 2012-09-24 08:30 +1000
    Re: List Problem "Littlefield, Tyler" <tyler@tysdomain.com> - 2012-09-23 18:56 -0600
    Re: List Problem Chris Angelico <rosuav@gmail.com> - 2012-09-24 11:52 +1000
  Re: List Problem Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-23 22:07 +0000
  Re: List Problem jimbo1qaz <jimmyli1528@gmail.com> - 2012-09-23 15:44 -0700

csiph-web