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


Groups > comp.lang.python > #12923

Re: I am confused by

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsreader4.netcologne.de!news.netcologne.de!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.014
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; '[0,': 0.09; 'from:addr:python': 0.09; '0],': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'module:': 0.16; 'received:84.92': 0.16; 'received:84.92.122': 0.16; 'received:84.92.122.60': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'reply-to:addr:python-list': 0.16; 'wrote:': 0.16; '>>>': 0.18; "doesn't": 0.22; 'header:In-Reply- To:1': 0.22; 'function': 0.27; 'martin': 0.28; 'received:84': 0.28; 'import': 0.28; "won't": 0.29; 'confused': 0.30; 'objects.': 0.30; 'list': 0.32; 'references': 0.32; 'objects': 0.32; 'to:addr :python-list': 0.33; 'header:User-Agent:1': 0.34; 'reply- to:addr:python.org': 0.34; 'themselves,': 0.34; 'using': 0.37; 'but': 0.37; 'some': 0.38; 'should': 0.38; 'subject:: ': 0.39; 'help': 0.39; 'to:addr:python.org': 0.39; 'called': 0.40; 'hope': 0.61; 'header:Reply-To:1': 0.71; 'reply-to:no real name:2**0': 0.71; '[[1,': 0.84; 'something.': 0.84
X-IronPort-Anti-Spam-Filtered true
X-IronPort-Anti-Spam-Result AqQHALr/Z05UXebj/2dsb2JhbABDmTCOQniBRgEBBAE4QBELCBAJFg8JAwIBAgENOBMIAQGHcQK4AoZrBIt1SYwGjAU
Date Thu, 08 Sep 2011 00:39:02 +0100
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:6.0.1) Gecko/20110830 Thunderbird/6.0.1
MIME-Version 1.0
To python-list@python.org
Subject Re: I am confused by
References <CAO_vtCZt1CTZnN9ss-X3RM5a=09PhkS0sHo00H4g3soeAQkKFw@mail.gmail.com>
In-Reply-To <CAO_vtCZt1CTZnN9ss-X3RM5a=09PhkS0sHo00H4g3soeAQkKFw@mail.gmail.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
Reply-To python-list@python.org
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.852.1315438747.27778.python-list@python.org> (permalink)
Lines 34
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1315438747 news.xs4all.nl 2410 [2001:888:2000:d::a6]:34317
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:12923

Show key headers only | View raw


On 07/09/2011 23:57, Martin Rixham wrote:
> Hi all
> I would appreciate some help understanding something. Basically I am
> confused by the following:
>
>  >>> a = [[0, 0], [0, 0]]
>  >>> b = list(a)
>  >>> b[0][0] = 1
>  >>> a
> [[1, 0], [0, 0]]
>
> I expected the last line to be
>
> [[0, 0], [0, 0]]
>
> I hope that's clear enough.
>
What you were expecting is called a "deep copy".

You should remember that a list doesn't contain objects themselves, but
only _references_ to objects.

"list" will copy the list itself (a "shallow copy"), including the
references which are in it, but it won't copy the _actual_ objects to
which they refer.

Try using the "deepcopy" function from the "copy" module:

 >>> from copy import deepcopy
 >>> a = [[0, 0], [0, 0]]
 >>> b = deepcopy(a)
 >>> b[0][0] = 1
 >>> a
[[0, 0], [0, 0]]

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


Thread

Re: I am confused by MRAB <python@mrabarnett.plus.com> - 2011-09-08 00:39 +0100

csiph-web