[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> </DIV>
<DIV><FONT face=3DArial size=3D2>>>> c1 =3D =
[[0]*2]*3<BR>>>>=20
c1<BR>[[0, 0], [0, 0], [0, 0]]<BR>>>> c2 =3D =
[[0,0]]*3<BR>>>>=20
c2<BR>[[0, 0], [0, 0], [0, 0]]<BR>>>> c3 =3D=20
[[0,0],[0,0],[0,0]]<BR>>>> c3<BR>[[0, 0], [0, 0], [0,=20
0]]<BR>>>> c1=3D=3Dc2=3D=3Dc3<BR>1<BR>>>> =
</FONT></DIV>
<DIV> </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> </DIV>
<DIV><FONT face=3DArial size=3D2>>>>=20
a=3D[[1,2,3],[3,4,5],[5,6,7]]<BR>>>>=20
b=3D[[1,1],[1,1],[1,1]]<BR>>>> i1=3D3<BR>>>> =
j1=3D2<BR>>>>=20
k1=3D3<BR>>>> for i in range(i1):<BR> for j in=20
range(j1):<BR> #c[i][j]=3D0 #not necessary if c is a zero=20
matrix<BR> for k in=20
range(k1):<BR> c[i][j]=3Dc[i][j]+a[i][k]*b[k][j]</FONT><=
/DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>Test 1:<BR>>>> c1 =3D=20
[[0]*2]*3<BR>>>> c1<BR>[[0, 0], [0, 0], [0, 0]]<BR>>>> =
c=3Dc1<BR>>>> c<BR>[[0, 0], [0, 0], [0, 0]]<BR>>>> for =
i in=20
range(i1):<BR> for j in=20
range(j1):<BR> #c[i][j]=3D0<BR> for k in=20
range(k1):<BR> c[i][j]=3Dc[i][j]+a[i][k]*b[k][j]</FONT><=
/DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2> <BR>>>> =
c<BR>[[36, 36],=20
[36, 36], [36, 36]]<BR>Test 2:<BR>>>> c2 =3D =
[[0,0]]*3<BR>>>>=20
c2<BR>[[0, 0], [0, 0], [0, 0]]<BR>>>> c=3Dc2<BR>>>> =
c<BR>[[0,=20
0], [0, 0], [0, 0]]<BR>>>> for i in range(i1):<BR> for j =
in=20
range(j1):<BR> #c[i][j]=3D0<BR> for k in=20
range(k1):<BR> c[i][j]=3Dc[i][j]+a[i][k]*b[k][j]</FONT><=
/DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2> <BR>>>> =
c<BR>[[36, 36],=20
[36, 36], [36, 36]]</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>Test 3:<BR>>>> c3 =3D=20
[[0,0],[0,0],[0,0]]<BR>>>> c3<BR>[[0, 0], [0, 0], [0,=20
0]]<BR>>>> c=3Dc3<BR>>>> c<BR>[[0, 0], [0, 0], [0,=20
0]]<BR>>>> for i in range(i1):<BR> for j in=20
range(j1):<BR> #c[i][j]=3D0<BR> for k in=20
range(k1):<BR> c[i][j]=3Dc[i][j]+a[i][k]*b[k][j]</FONT><=
/DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2> <BR>>>> =
c<BR>[[6, 6],=20
[12, 12], [18, 18]]<BR>>>> </FONT></DIV>
<DIV> </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 Hugh</FONT></DIV></BODY></HTML>
------=_NextPart_000_0009_01C1F02E.D82A57A0--