Licheng Fang wrote: >Dennis Lee Bieber wrote: > > >>On 26 Apr 2006 01:13:20 -0700, "Licheng Fang" <[EMAIL PROTECTED]> >>declaimed the following in comp.lang.python: >> >> >> >> >>>Could anybody please explain to me why three values were change? I'm >>>bewildered. Thanks! >>> >>> >>http://www.aifb.uni-karlsruhe.de/Lehrangebot/Winter2000-01/E-Shop/Python/Doku/The%20Whole%20Python%20FAQ.htm#4.50 >>-- >> > ============================================================== < >> > [EMAIL PROTECTED] | Wulfraed Dennis Lee Bieber KD6MOG < >> > [EMAIL PROTECTED] | Bestiaria Support Staff < >> > ============================================================== < >> > Home Page: <http://www.dm.net/~wulfraed/> < >> > Overflow Page: <http://wlfraed.home.netcom.com/> < >> >> > >Thank you very much! > >But I still wonder why a nested assignment "a = [[0]*3]*3" generates 3 >references to the same list, while the commands below apparently do >not. > > > It's got nothing to do with "nested"-ness. If X is ANY object in Python, then [X]*3 will make a list with three references to X. If you don't want three references to the single object X (and you don't), then you have to find a different way to create three separate objects. The copy module helps with this in some cases, but for your simple example, you just want to create the three inner objects by evaluating the expression [0]*3 three times. Here's several ways:
a = [] for i in range(3): a.append([0]*3) or a = [ [0]*3 for i in range(3)] Clear? Gary Herron -- http://mail.python.org/mailman/listinfo/python-list