[Tutor] Question about nested FOR looping

stuart_clemons@us.ibm.com stuart_clemons@us.ibm.com
Wed Apr 30 14:23:19 2003

```This is a multipart message in MIME format.
--=_alternative 0065138C85256D18_=
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: quoted-printable

Thanks Bob and Magnus.  You've expanded my Python knowledge !

So, as a follow-up out of curiosity, which is the most efficient:

1) Read the two files into lists and then process the lists, or

2) Process directly from the file reads, using seek in the nested FOR, or

3) Read one file into a list and process during the read of the second=20
file ?

As some added info, Scott Widney from tutor@python.org, provided a=20
dictionary construct for this same problem, ie, compare two lists,=20
identifying the duplicates.   I would guess that the dictionary construct=20
would be the most efficient method, based on random vs sequential=20
ordering.  Is this right ?

(BTW, I used the dictionary construct to provide a report I needed to=20
produce quickly.  As a learning exercise, I've now gone back to look at my =

original approach to the problem, which I couldn't get to work at the=20
time.)

- Stuart
=20

Magnus Lyck=E5 <magnus@thinkware.se>
04/30/2003 12:22 PM
=20
To:     Bob Gailer <bgailer@alum.rpi.edu>,=20
stuart=5Fclemons@us.ibm.com, tutor@python.org
cc:=20
Subject:        Re: [Tutor] Question about nested FOR looping

Bob Gailer wrote in response to Stuart's:
>>Why are they different ?
>
>Repeating "for s in subitems:" reuses the list each time
>The first call to "for s in sF.readlines():" reads to end of file.=20
>Subsequent calls do NOT move to start of file; just return ''.

if you do "sF.seek(0)" before "for s in sF.readlines():", i.e.
reset the current file position to the beginning, it will work
as you expect.

BTW, with a modern python you can simply write "for s in sF:".
Calling .readlines() is implied in that context.

--
Magnus Lycka (It's really Lyck&aring;), magnus@thinkware.se
Thinkware AB, Sweden, www.thinkware.se
I code Python ~ The shortest path from thought to working program=20

--=_alternative 0065138C85256D18_=
Content-Type: text/html; charset="ISO-8859-1"
Content-Transfer-Encoding: quoted-printable

<br><font size=3D2 face=3D"sans-serif">Thanks Bob and Magnus. &nbsp;You've
expanded my Python knowledge !</font>
<br>
<br><font size=3D2 face=3D"sans-serif">So, as a follow-up out of curiosity,
which is the most efficient:</font>
<br>
<br><font size=3D2 face=3D"sans-serif">1) Read the two files into lists and
then process the lists, or</font>
<br>
<br><font size=3D2 face=3D"sans-serif">2) Process directly from the file re=
using seek in the nested FOR, or</font>
<br>
<br><font size=3D2 face=3D"sans-serif">3) Read one file into a list and pro=
cess
during the read of the second file ?</font>
<br>
<br><font size=3D2 face=3D"sans-serif">As some added info, Scott Widney from
tutor@python.org, provided a dictionary construct for this same problem,
ie, compare two lists, identifying the duplicates. &nbsp; I would guess
that the dictionary construct would be the most efficient method, based
on random vs sequential ordering. &nbsp;Is this right ?</font>
<br>
<br><font size=3D2 face=3D"sans-serif">(BTW, I used the dictionary construct
to provide a report I needed to produce quickly. &nbsp;As a learning exerci=
se,
I've now gone back to look at my original approach to the problem, which
I couldn't get to work at the time.)</font>
<br>
<br><font size=3D2 face=3D"sans-serif">Thanks again for your responses.</fo=
nt>
<br>
<br><font size=3D2 face=3D"sans-serif">- Stuart</font>
<br><font size=3D2 face=3D"sans-serif">&nbsp; </font>
<br>
<br>
<br>
<table width=3D100%>
<tr valign=3Dtop>
<td>
<td><font size=3D1 face=3D"sans-serif"><b>Magnus Lyck=E5 &lt;magnus@thinkwa=
re.se&gt;</b></font>
<p><font size=3D1 face=3D"sans-serif">04/30/2003 12:22 PM</font>
<td><font size=3D1 face=3D"Arial">&nbsp; &nbsp; &nbsp; &nbsp; </font>
<br><font size=3D1 face=3D"sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; To:
&nbsp; &nbsp; &nbsp; &nbsp;Bob Gailer &lt;bgailer@alum.rpi.edu&gt;,
stuart=5Fclemons@us.ibm.com, tutor@python.org</font>
<br><font size=3D1 face=3D"sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; cc:
&nbsp; &nbsp; &nbsp; &nbsp;</font>
<br><font size=3D1 face=3D"sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; Subject:
&nbsp; &nbsp; &nbsp; &nbsp;Re: [Tutor] Question about nested FOR
looping</font></table>
<br>
<br>
<br><font size=3D2><tt>Bob Gailer wrote in response to Stuart's:<br>
&gt;&gt;Why are they different ?<br>
&gt;<br>
&gt;Repeating &quot;for s in subitems:&quot; reuses the list each time<br>
of file. <br>
&gt;Subsequent calls do NOT move to start of file; just return ''.<br>
<br>
if you do &quot;sF.seek(0)&quot; before &quot;for s in sF.readlines():&quot=
;,
i.e.<br>
reset the current file position to the beginning, it will work<br>
as you expect.<br>
<br>
BTW, with a modern python you can simply write &quot;for s in sF:&quot;.<br>
Calling .readlines() is implied in that context.<br>
<br>
<br>
--<br>
Magnus Lycka (It's really Lyck&amp;aring;), magnus@thinkware.se<br>
Thinkware AB, Sweden, www.thinkware.se<br>
I code Python ~ The shortest path from thought to working program <br>
<br>
</tt></font>
<br>
--=_alternative 0065138C85256D18_=--

```