473,218 Members | 1,696 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,218 software developers and data experts.

Syntax Question - list multiplication

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

Aug 19 '07 #1
5 1495
MC
Classic

--
@-salutations

Michel Claveau
Aug 19 '07 #2
On Sunday 19 August 2007, Pablo Torres wrote:
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-----

Aug 19 '07 #3
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
--
If I have been able to see further, it was only because I stood
on the shoulders of giants. -- Isaac Newton

Roel Schroeven
Aug 19 '07 #4
Thanks everyone. Now I see why every row in my grid were actually the
same object.

Pablo

Aug 19 '07 #5
Roel Schroeven wrote:
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
Aug 20 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: Steven Bethard | last post by:
I think one of the biggest reasons we're having such problems coming to any agreement on decorator syntax is that each proposal makes a number of syntax decisions, not just one. For decorators, I...
1
by: Nils Grimsmo | last post by:
i always have trouble explaining why creation of multidimensional lists is not as straight forward as it could be in python. list comprehensions are ugly if you are new to the language. i really...
19
by: Nicolas Fleury | last post by:
Hi everyone, I would to know what do you think of this PEP. Any comment welcomed (even about English mistakes). PEP: XXX Title: Specialization Syntax Version: $Revision: 1.10 $...
54
by: Andy | last post by:
Hi, I don't know if this is the correct group to post this, but when I multiply a huge floating point value by a really small (non-zero) floating point value, I get 0 (zero) for the result. This...
9
by: Ralf Hildebrandt | last post by:
Hi all! First of all: I am a C-newbie. I have noticed a "strange" behavior with the standart integer multiplication. The code is: void main(void)
87
by: Vijay Kumar R Zanvar | last post by:
Hi, Why multiplication of pointers is not allowed? Till now I only know this, but not the reason why! PS: As a rule, I searched the FAQ, but could not find an answer. -- Vijay Kumar R...
17
by: Christopher Dyken | last post by:
Hi group, I'm trying to implement two routines to handle 32x32-bits and 64x64-bits signed integer multiplication on a 32 bits machine in C. It easy to find descriptions of non-signed...
23
by: mahesh | last post by:
Hi all, I have following code that is supposed to increase the power by specified value. int main() { system("cls"); int i, exponent; double base; double new_base=0.0;
1
by: Sozos | last post by:
Hi guys. I have a problem with writing the base case for the following matrix multiplication function I have implemented. Please help. #define index(i,j,power) (((i)<<(power))+(j)) void...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.