[Tutor] regarding the list problem

Kang, Yang Jae kangyangjae at gmail.com
Wed Feb 6 10:48:10 CET 2013


Hello

 

I'm a beginner to python. I ran following code and expected [[1, 0], [0, 0],
[0, 0]]

However unexpected result came up. Anybody who can teach me why and how to
solve?

 

Python 3.2.3 (default, May 19 2012, 23:34:41) 

[GCC 4.4.3] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> [1] *3

[1, 1, 1]

>>> [[0,0]]*3

[[0, 0], [0, 0], [0, 0]]

>>> a = [[0,0]]*3

>>> a

[[0, 0], [0, 0], [0, 0]]

>>> a[0][0] += 1

>>> a

[[1, 0], [1, 0], [1, 0]]

 

Kang, Yang Jae 

Ph.D.

Cropgenomics Lab.

College of Agriculture and Life Science

Seoul National University 

Korea

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130206/770c3b07/attachment-0001.html>


More information about the Tutor mailing list