Re: [Tutor] Matrix bug

2015-04-05 Thread Steven D'Aprano
On Sun, Apr 05, 2015 at 11:12:32AM -0300, Narci Edson Venturini wrote: > The next code has an unexpected result: > > >>>a=3*[3*[0]] > >>>a > [[0, 0, 0], [0, 0, 0], [0, 0, 0]] > >>>a[0][0]=1 > >>>a > [[1, 0, 0], [1, 0, 0], [1, 0, 0]] It isn't obvious, and it is *very* common for people to run into

Re: [Tutor] Matrix bug

2015-04-05 Thread Alan Gauld
On 05/04/15 15:12, Narci Edson Venturini wrote: The next code has an unexpected result: a=3*[3*[0]] Note that this makes three references to the list of 3 references to 0. In other words you reference the same list 3 times. So when you change the first copy you change the other 2 also. Put a

Re: [Tutor] Matrix bug

2015-04-05 Thread Emile van Sebille
On 4/5/2015 7:12 AM, Narci Edson Venturini wrote: The next code has an unexpected result: a=3*[3*[0]] a now contains three references to the same object, hence the results you show below. You can create three distinct objects as follows: >>> a = [ [0,0,0] for i in (0,1,2) ] >>> a[1][1]=1

[Tutor] Matrix bug

2015-04-05 Thread Narci Edson Venturini
The next code has an unexpected result: >>>a=3*[3*[0]] >>>a [[0, 0, 0], [0, 0, 0], [0, 0, 0]] >>>a[0][0]=1 >>>a [[1, 0, 0], [1, 0, 0], [1, 0, 0]] The code assigned to "1" a(0,0), a(1,0) and a(2,0). It was expected: [[1, 0, 0], [0, 0, 0], [0, 0, 0]] When the followind code is ran, them the corre