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
>>> a
[[0, 0, 0], [0, 1, 0], [0, 0, 0]]
>>>

hth,

Emile



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 correct result is obtained:

a=[[0 for i in range(3)] for j in range(3)]
a
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
a[0][0]=1
a
  [[1, 0, 0], [0, 0, 0], [0, 0, 0]]

So, what is wrong ?

Best Regars,



Narci
__________________
Narci Edson Venturini
(19) 99733-8420
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor



_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to