[Tutor] Nested list anomaly

Hugh Stewart Hugh Stewart" <hughstewart@optushome.com.au
Tue, 30 Apr 2002 10:07:32 +1000


This is a multi-part message in MIME format.

------=_NextPart_000_0009_01C1F02E.D82A57A0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hi Folk,
I was toying with the idea of producing a suite of=20
matrix operations using list comprehensions. At one point
I needed to initialize a matrix so all elements were zero.
Three possiblities came to mind:

>>> c1 =3D [[0]*2]*3
>>> c1
[[0, 0], [0, 0], [0, 0]]
>>> c2 =3D [[0,0]]*3
>>> c2
[[0, 0], [0, 0], [0, 0]]
>>> c3 =3D [[0,0],[0,0],[0,0]]
>>> c3
[[0, 0], [0, 0], [0, 0]]
>>> c1=3D=3Dc2=3D=3Dc3
1
>>>=20

I believed these three matrices c1,c2 and c3 were equivalent,
but when used in the following simple matrix multipication=20
algorithm it became evident c1 and c2 are not the 'same' as c3.

>>> a=3D[[1,2,3],[3,4,5],[5,6,7]]
>>> b=3D[[1,1],[1,1],[1,1]]
>>> i1=3D3
>>> j1=3D2
>>> k1=3D3
>>> for i in range(i1):
 for j in range(j1):
  #c[i][j]=3D0 #not necessary if c is a zero matrix
  for k in range(k1):
   c[i][j]=3Dc[i][j]+a[i][k]*b[k][j]

Test 1:
>>> c1 =3D [[0]*2]*3
>>> c1
[[0, 0], [0, 0], [0, 0]]
>>> c=3Dc1
>>> c
[[0, 0], [0, 0], [0, 0]]
>>> for i in range(i1):
 for j in range(j1):
  #c[i][j]=3D0
  for k in range(k1):
   c[i][j]=3Dc[i][j]+a[i][k]*b[k][j]

  =20
>>> c
[[36, 36], [36, 36], [36, 36]]
Test 2:
>>> c2 =3D [[0,0]]*3
>>> c2
[[0, 0], [0, 0], [0, 0]]
>>> c=3Dc2
>>> c
[[0, 0], [0, 0], [0, 0]]
>>> for i in range(i1):
 for j in range(j1):
  #c[i][j]=3D0
  for k in range(k1):
   c[i][j]=3Dc[i][j]+a[i][k]*b[k][j]

  =20
>>> c
[[36, 36], [36, 36], [36, 36]]

Test 3:
>>> c3 =3D [[0,0],[0,0],[0,0]]
>>> c3
[[0, 0], [0, 0], [0, 0]]
>>> c=3Dc3
>>> c
[[0, 0], [0, 0], [0, 0]]
>>> for i in range(i1):
 for j in range(j1):
  #c[i][j]=3D0
  for k in range(k1):
   c[i][j]=3Dc[i][j]+a[i][k]*b[k][j]

  =20
>>> c
[[6, 6], [12, 12], [18, 18]]
>>>=20

It is only the matrix c3 which yields the correct result.
Can any of you gurus explain what is an anomaly to me.
Thanks  Hugh

------=_NextPart_000_0009_01C1F02E.D82A57A0
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content=3D"text/html; charset=3Diso-8859-1" =
http-equiv=3DContent-Type>
<META content=3D"MSHTML 5.00.2614.3500" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#fffff0>
<DIV><FONT face=3DArial size=3D2>Hi Folk,<BR>I was toying with the idea =
of producing=20
a suite of <BR>matrix operations using list comprehensions. At one =
point<BR>I=20
needed to initialize a matrix so all elements were zero.<BR>Three =
possiblities=20
came to mind:</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>&gt;&gt;&gt; c1 =3D =
[[0]*2]*3<BR>&gt;&gt;&gt;=20
c1<BR>[[0, 0], [0, 0], [0, 0]]<BR>&gt;&gt;&gt; c2 =3D =
[[0,0]]*3<BR>&gt;&gt;&gt;=20
c2<BR>[[0, 0], [0, 0], [0, 0]]<BR>&gt;&gt;&gt; c3 =3D=20
[[0,0],[0,0],[0,0]]<BR>&gt;&gt;&gt; c3<BR>[[0, 0], [0, 0], [0,=20
0]]<BR>&gt;&gt;&gt; c1=3D=3Dc2=3D=3Dc3<BR>1<BR>&gt;&gt;&gt; =
</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>I believed these three matrices c1,c2 =
and c3 were=20
equivalent,<BR>but when used in the following simple matrix =
multipication=20
<BR>algorithm it became evident c1 and c2 are not the 'same' as =
c3.</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>&gt;&gt;&gt;=20
a=3D[[1,2,3],[3,4,5],[5,6,7]]<BR>&gt;&gt;&gt;=20
b=3D[[1,1],[1,1],[1,1]]<BR>&gt;&gt;&gt; i1=3D3<BR>&gt;&gt;&gt; =
j1=3D2<BR>&gt;&gt;&gt;=20
k1=3D3<BR>&gt;&gt;&gt; for i in range(i1):<BR>&nbsp;for j in=20
range(j1):<BR>&nbsp;&nbsp;#c[i][j]=3D0 #not necessary if c is a zero=20
matrix<BR>&nbsp;&nbsp;for k in=20
range(k1):<BR>&nbsp;&nbsp;&nbsp;c[i][j]=3Dc[i][j]+a[i][k]*b[k][j]</FONT><=
/DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Test 1:<BR>&gt;&gt;&gt; c1 =3D=20
[[0]*2]*3<BR>&gt;&gt;&gt; c1<BR>[[0, 0], [0, 0], [0, 0]]<BR>&gt;&gt;&gt; =

c=3Dc1<BR>&gt;&gt;&gt; c<BR>[[0, 0], [0, 0], [0, 0]]<BR>&gt;&gt;&gt; for =
i in=20
range(i1):<BR>&nbsp;for j in=20
range(j1):<BR>&nbsp;&nbsp;#c[i][j]=3D0<BR>&nbsp;&nbsp;for k in=20
range(k1):<BR>&nbsp;&nbsp;&nbsp;c[i][j]=3Dc[i][j]+a[i][k]*b[k][j]</FONT><=
/DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp;<BR>&gt;&gt;&gt; =
c<BR>[[36, 36],=20
[36, 36], [36, 36]]<BR>Test 2:<BR>&gt;&gt;&gt; c2 =3D =
[[0,0]]*3<BR>&gt;&gt;&gt;=20
c2<BR>[[0, 0], [0, 0], [0, 0]]<BR>&gt;&gt;&gt; c=3Dc2<BR>&gt;&gt;&gt; =
c<BR>[[0,=20
0], [0, 0], [0, 0]]<BR>&gt;&gt;&gt; for i in range(i1):<BR>&nbsp;for j =
in=20
range(j1):<BR>&nbsp;&nbsp;#c[i][j]=3D0<BR>&nbsp;&nbsp;for k in=20
range(k1):<BR>&nbsp;&nbsp;&nbsp;c[i][j]=3Dc[i][j]+a[i][k]*b[k][j]</FONT><=
/DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp;<BR>&gt;&gt;&gt; =
c<BR>[[36, 36],=20
[36, 36], [36, 36]]</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Test 3:<BR>&gt;&gt;&gt; c3 =3D=20
[[0,0],[0,0],[0,0]]<BR>&gt;&gt;&gt; c3<BR>[[0, 0], [0, 0], [0,=20
0]]<BR>&gt;&gt;&gt; c=3Dc3<BR>&gt;&gt;&gt; c<BR>[[0, 0], [0, 0], [0,=20
0]]<BR>&gt;&gt;&gt; for i in range(i1):<BR>&nbsp;for j in=20
range(j1):<BR>&nbsp;&nbsp;#c[i][j]=3D0<BR>&nbsp;&nbsp;for k in=20
range(k1):<BR>&nbsp;&nbsp;&nbsp;c[i][j]=3Dc[i][j]+a[i][k]*b[k][j]</FONT><=
/DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp;<BR>&gt;&gt;&gt; =
c<BR>[[6, 6],=20
[12, 12], [18, 18]]<BR>&gt;&gt;&gt; </FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>It is only the matrix c3 which yields =
the correct=20
result.<BR>Can any of you gurus explain what is an anomaly to=20
me.<BR>Thanks&nbsp; Hugh</FONT></DIV></BODY></HTML>

------=_NextPart_000_0009_01C1F02E.D82A57A0--