an enumerate question

Terry Hancock hancock at anansispaceworks.com
Wed Mar 21 06:03:27 EDT 2007


eight02645999 at yahoo.com wrote:
> say i want to enumerate lines of a file
> eg
> for n,l in enumerate(open("file")):
>      # print  next line ie

I think you'd find it much easier to move your frame of reference one
line forward and think in terms of remembering the previous line, e.g.:

for n,curr in enumerate(open("file")):
    if n>1:
        print n,curr
        print m,prev
    m,prev = n,curr

Of course, if the file isn't so big, then you could use readlines as you
mention.

Cheers,
Terry

-- 
Terry Hancock (hancock at AnansiSpaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com




More information about the Python-list mailing list