REPOST: Re: is this a bug or what?

Chris Liechti cliechti at gmx.net
Fri Dec 28 08:27:49 EST 2001


[posted and mailed]

"I.J." <shiver at yubc.net> wrote in news:a0dv4v$bnh$1 at cer.yubc.net:
>>>> M=[[0]*3]*3
>>>> M
> [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
>>>> M[1][1]=1
>>>> M
> [[0, 1, 0], [0, 1, 0], [0, 1, 0]]
> 
> Is this new age math?

no your multiplying the reference to one list [0,0,0]. all three sublists 
are the same object.
it works well with strings, numbers and tuples because these are immutable 
an when you change someting a new object is generated.

e.g. 
l = [0]*4
l[2] = 1

a list like in your example can nicly be created by list comprehesions:
M = [ [0]*3 for i in range(3) ]

-- 
Chris <cliechti at gmx.net>

========= WAS CANCELLED BY =======:
Path: news.sol.net!spool1-nwblwi.newsops.execpc.com!newsfeeds.sol.net!newspump.sol.net!newsfeed.direct.ca!look.ca!newshub2.rdc1.sfba.home.com!news.home.com!sjc1.nntp.concentric.net!newsfeed.concentric.net!newsfeed.ozemail.com.au!ozemail.com.au!not-for-mail
Message-ID: <cancel.Xns9185938C6F94Bcliechtigmxnet at 62.2.16.82>
Control: cancel <Xns9185938C6F94Bcliechtigmxnet at 62.2.16.82>
Subject: cmsg cancel <Xns9185938C6F94Bcliechtigmxnet at 62.2.16.82>
From: Chris Liechti <cliechti at gmx.net>
Newsgroups: comp.lang.python
X-No-Archive: yes
Lines: 2
NNTP-Posting-Host: wonenara.ozemail.com.au
X-Trace: ozemail.com.au 1009643019 203.108.164.177 (Sun, 30 Dec 2001 03:23:39 EST)
NNTP-Posting-Date: Sun, 30 Dec 2001 03:23:39 EST
Organization: OzEmail Ltd, Australia
Distribution: world
Date: Sat, 29 Dec 2001 14:27:59 GMT

This message was cancelled from within Mozilla.



More information about the Python-list mailing list