[Tutor] A couple questions about lists
Glen Wheeler
wheelege@tsn.cc
Sat, 28 Apr 2001 17:46:38 +1000
This is a multi-part message in MIME format.
------=_NextPart_000_00F5_01C0D00B.2CD750E0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
I had some variables I wanted to do similar operations on, so i put =
them in a list and sent them through a for loop:
for x in [a,b,c,d,e]:
if x<10:
x=3Dx+10
After running this the list and the variables all had the same =
values, but x was equal to 15(the value of e). Is there a way to change =
part of a list with using it's index? Either way, I figured out that I =
could use
list=3D[a,b,c,d,e]
for x in range(0,4):
if list[x]<10:
list[x]=3Dlist[x]+10
But it only changed the values in the list, and didn't change the =
variables. Is there a way to make a list, or some other object, that =
keeps a reference to the variable instead of just taking it's value?
Okay, so you want to get some variables, put them in a list and make a =
change to a member of the list change the variable that was its model?
First off, I've got to ask why you don't just use lists and give up on =
the single variables? For example...
>>> x =3D 1
>>> y =3D 2
>>> z =3D 3
>>> l =3D [x, y, z]
>>> l
[1, 2, 3]
>>> l[0]
1
>>> l[1]
2
>>> l[2]
3
Now, how is writing l[0] different to writing x? Except for the two =
extra key presses, I don't see any disadvantages to storing it this way. =
Then something like...
>>> for index in range(len(l)):
... l[index] =3D l[index] + 5
...=20
>>> l
[6, 7, 8]
>>> l[0]
6
>>> l[1]
7
>>> l[2]
8
Will work fine, and you don't need to worry about it. I only say this =
because the only way I can see to get back to the x,y,z variables is by =
direct assignment - ie x =3D l[0], y =3D l[1], z =3D l[2]. Which just =
adds another step when you can directly reference them by just writing =
l[0] etc.
Food for thought,
Glen.
------=_NextPart_000_00F5_01C0D00B.2CD750E0
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 http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 5.50.4522.1800" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV>
<BLOCKQUOTE=20
style=3D"PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; =
BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
<DIV>I had some variables I wanted to do similar operations on, so i =
put them=20
in a list and sent them through a for loop:<BR>for x in=20
=
[a,b,c,d,e]:<BR><X-TAB> </=
X-TAB>if=20
=
x<10:<BR><X-TAB> </X-TA=
B><X-TAB> </X-TAB>x=3Dx+10=
</DIV>
<DIV><BR> After running this the list and the variables all had =
the same=20
values, but x was equal to 15(the value of e). I<U>s there a way to =
change=20
part of a list with using it's index?</U> Either way, I figured =
out that=20
I could use<BR>list=3D[a,b,c,d,e]<BR>for x in=20
=
range(0,4):<BR><X-TAB> </X=
-TAB>if=20
=
list[x]<10:<BR><X-TAB> =
</X-TAB><X-TAB> </X-TAB>li=
st[x]=3Dlist[x]+10<BR>But=20
it only changed the values in the list, and didn't change the =
variables. =20
I<U>s there a way to make a list, or some other object, that keeps a =
reference=20
to the variable instead of just taking it's =
value?</U></DIV></BLOCKQUOTE></DIV>
<DIV> Okay, so you want to get some variables, put them in a list =
and make=20
a change to a member of the list change the variable that was its =
model?</DIV>
<DIV> First off, I've got to ask why you don't just use lists and =
give up=20
on the single variables? For example...</DIV>
<DIV> </DIV>
<DIV>>>> x =3D 1<BR>>>> y =3D 2<BR>>>> z =3D=20
3<BR>>>> l =3D [x, y, z]<BR>>>> l<BR>[1, 2, =
3]<BR>>>>=20
l[0]<BR>1<BR>>>> l[1]<BR>2<BR>>>> l[2]<BR>3<BR></DIV>
<DIV> Now, how is writing l[0] different to writing x? =
Except for=20
the two extra key presses, I don't see any disadvantages to storing it =
this=20
way. Then something like...</DIV>
<DIV> </DIV>
<DIV>>>> for index in range(len(l)):<BR>... l[index] =3D =
l[index] +=20
5<BR>... <BR>>>> l<BR>[6, 7, 8]<BR>>>>=20
l[0]<BR>6<BR>>>> l[1]<BR>7<BR>>>> l[2]<BR>8</DIV>
<DIV> </DIV>
<DIV> Will work fine, and you don't need to worry about it. =
I only=20
say this because the only way I can see to get back to the x,y,z =
variables=20
is by direct assignment - ie x =3D l[0], y =3D l[1], z =3D l[2]. =
Which just adds=20
another step when you can directly reference them by just writing l[0]=20
etc.</DIV>
<DIV> </DIV>
<DIV> Food for thought,</DIV>
<DIV> Glen.</DIV></BODY></HTML>
------=_NextPart_000_00F5_01C0D00B.2CD750E0--