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


Groups > comp.lang.python > #12924

Re: I am confused by

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <gherron@digipen.edu>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.079
X-Spam-Evidence '*H*': 0.84; '*S*': 0.00; '[0,': 0.09; 'library': 0.15; '"b"': 0.16; '0],': 0.16; 'does,': 0.16; 'wrote:': 0.16; '>>>': 0.18; 'header:In-Reply-To:1': 0.22; 'pm,': 0.24; 'martin': 0.28; 'confused': 0.30; 'objects': 0.32; 'there': 0.33; 'to:addr :python-list': 0.33; 'header:User-Agent:1': 0.34; 'assignment': 0.34; 'lists,': 0.35; 'list,': 0.37; 'but': 0.37; 'some': 0.38; 'subject:: ': 0.39; 'help': 0.39; 'science': 0.39; 'to:addr:python.org': 0.39; 'hope': 0.61; 'received:10.10': 0.68; '[[1,': 0.84; 'something.': 0.84
Date Wed, 07 Sep 2011 16:31:37 -0700
From Gary Herron <gherron@digipen.edu>
User-Agent Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.21) Gecko/20110831 Thunderbird/3.1.13
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
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.853.1315438788.27778.python-list@python.org> (permalink)
Lines 36
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1315438788 news.xs4all.nl 2401 [2001:888:2000:d::a6]:34773
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:12924

Show key headers only | View raw


On 09/07/2011 03:57 PM, 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.
>
> Martin

You were expecting the assignment to "b" to create a copy of the 
original list, and it does, but the copy is only one level deep.

So a and b are different lists, but a[i] and b[i] are the same objects 
for each i.

There is a "deepcopy" in the library which may do what you expect.

Gary Herron


-- 
Gary Herron, PhD.
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418

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


Thread

Re: I am confused by Gary Herron <gherron@digipen.edu> - 2011-09-07 16:31 -0700

csiph-web