[Tutor] Fastest way to iterate through a file
Alan Gauld
alan.gauld at btinternet.com
Fri Jun 29 18:10:06 CEST 2007
"Robert Hicks" <sigzero at gmail.com> wrote
> This is the loop code:
>
> for line in f2:
> for id in idList:
> if id in line:
> print "%s: %s" % (id, f2.next())
> found = "%s: %s" % (id, f2.next())
> f3.write(found)
>
While I note that you got a solution using regex one general
point worth noting is that you should, in nested loop cases like
this, at least use break in the inner loop.
Here you search the line for each id - 129 searches, even
if you find it first time. Thus adding a break inside the if block
should save, on average 60 if tests. And given each test
involves iterating over the line thats significant!
Alan G.
(Whos been away for a week)
More information about the Tutor
mailing list