Syntax Question - list multiplication

P

Pablo Torres

Hi guys!

I am working on Conway's Game of Life right now and I've run into a
little problem.
I represent dead cells with 0s and live ones with 1s. Check this out:

>>> grid = [[0] * 3] * 3
>>> grid
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
>>> grid[0][0] = 1
[[1, 0, 0], [1, 0, 0], [1, 0, 0]]

Now, that's not what I want. I just want the first element of the
first sublist to be a 1, not the first of every one. Here's what I
tried and didn't work:

0. grid = [[0] * 3][:] * 3 then the same grid[0][0] = 1 statement
1. grid = [[0][:] * 3] * 3 followed by the same assignment
2. (grid[0])[0] = 1 with the original initialization of the grid

So, that means that it isn't a problem with two different variables
refering to the same list (0. and 1. prove that). What I don't
understand is why 2. doesn't work either. I'm baby-feeding my
instructions to Python and the mistake is still there. Any ideas?

Hope you can help. Thanks in advance,

Pablo
 
T

Thomas Jollans

Hi guys!

I am working on Conway's Game of Life right now and I've run into a
little problem.

I represent dead cells with 0s and live ones with 1s. Check this out:
>>> grid = [[0] * 3] * 3
>>> grid

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

>>> grid[0][0] = 1

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

Now, that's not what I want. I just want the first element of the
first sublist to be a 1, not the first of every one. Here's what I
tried and didn't work:

0. grid = [[0] * 3][:] * 3 then the same grid[0][0] = 1 statement
1. grid = [[0][:] * 3] * 3 followed by the same assignment
2. (grid[0])[0] = 1 with the original initialization of the grid

So, that means that it isn't a problem with two different variables
refering to the same list (0. and 1. prove that). What I don't
understand is why 2. doesn't work either. I'm baby-feeding my
instructions to Python and the mistake is still there. Any ideas?


If you want three copies of the list, you need to copy it thrice (well, twice)

Thus:
row = [0] * 3
grid = []
for n in xrange(3): grid.append(row[:]) ...
grid [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
grid[0][0] =1
grid [[1, 0, 0], [0, 0, 0], [0, 0, 0]]


--
Regards, Thomas Jollans
GPG key: 0xF421434B may be found on various keyservers, eg pgp.mit.edu
Hacker key <http://hackerkey.com/>:
v4sw6+8Yhw4/5ln3pr5Ock2ma2u7Lw2Nl7Di2e2t3/4TMb6HOPTen5/6g5OPa1XsMr9p-7/-6

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQBGyMbtJpinDvQhQ0sRApTqAJ4m/bhLdNtr9XGjBhipvhYQ7T4H0gCfXmtW
/+c21FwvXIth2/oXpP48Afw=
=tbqy
-----END PGP SIGNATURE-----
 
R

Roel Schroeven

Pablo Torres schreef:
Hi guys!

I am working on Conway's Game of Life right now and I've run into a
little problem.
I represent dead cells with 0s and live ones with 1s. Check this out:

>>> grid = [[0] * 3] * 3
>>> grid
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
>>> grid[0][0] = 1
[[1, 0, 0], [1, 0, 0], [1, 0, 0]]

Now, that's not what I want. I just want the first element of the
first sublist to be a 1, not the first of every one. Here's what I
tried and didn't work:

0. grid = [[0] * 3][:] * 3 then the same grid[0][0] = 1 statement
1. grid = [[0][:] * 3] * 3 followed by the same assignment
2. (grid[0])[0] = 1 with the original initialization of the grid

So, that means that it isn't a problem with two different variables
refering to the same list (0. and 1. prove that). What I don't
understand is why 2. doesn't work either. I'm baby-feeding my
instructions to Python and the mistake is still there. Any ideas?

Hope you can help. Thanks in advance,

The multiplication operator doesn't make new copies; all elements
reference the same object, as you can see with id():
>>> lst = [0] * 3
>>> lst [0, 0, 0]
>>> id(lst[0]), id(lst[1]), id(lst[2])
(9788716, 9788716, 9788716)

As a consequence, if you modify one element, you change them all (since
it's all the same object). Solution: make sure to create independent lists.
>>> grid = [[0] * 3 for i in range(3)]
>>> grid [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
>>> grid[0][0] = 1
 
P

Pablo Torres

Thanks everyone. Now I see why every row in my grid were actually the
same object.

Pablo
 
D

Duncan Smith

Roel said:
Pablo Torres schreef:
Hi guys!

I am working on Conway's Game of Life right now and I've run into a
little problem.
I represent dead cells with 0s and live ones with 1s. Check this out:
grid = [[0] * 3] * 3
grid
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
grid[0][0] = 1
[[1, 0, 0], [1, 0, 0], [1, 0, 0]]

Now, that's not what I want. I just want the first element of the
first sublist to be a 1, not the first of every one. Here's what I
tried and didn't work:

0. grid = [[0] * 3][:] * 3 then the same grid[0][0] = 1 statement
1. grid = [[0][:] * 3] * 3 followed by the same assignment
2. (grid[0])[0] = 1 with the original initialization of the grid

So, that means that it isn't a problem with two different variables
refering to the same list (0. and 1. prove that). What I don't
understand is why 2. doesn't work either. I'm baby-feeding my
instructions to Python and the mistake is still there. Any ideas?

Hope you can help. Thanks in advance,


The multiplication operator doesn't make new copies; all elements
reference the same object, as you can see with id():
lst = [0] * 3
lst [0, 0, 0]
id(lst[0]), id(lst[1]), id(lst[2])
(9788716, 9788716, 9788716)

I think you probably meant something like,
lst = [[0] * 3] * 3
lst [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
id(lst[0]), id(lst[1]), id(lst[2]) (15937704, 15937704, 15937704)

i.e. a list of mutable types where the difference between multiple
objects and multiple references to the same object will be apparent.
With small integers you're likely to find you have multiple references
to the same integer objects however the list is constructed.

Duncan
 
J

James Stroud

MC said:

Thanks Michel, but maybe this bit of programming jargon needs some
translation for the uninitiated:

Classic \Clas"sic\ (kl[a^]s"s[i^]k), Classical \Clas"sic*al\, a.
0. Read the FAQ
1. First rate
2. Greek
3. Refined

James


--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top