Question on loops

Garry Knight garryknight at gmx.net
Mon Mar 8 18:33:41 EST 2004


In message <e0bff448.0403080640.24f76dec at posting.google.com>, Daniel H
Neilson wrote:

> "Delaney, Timothy C (Timothy)" <tdelaney at avaya.com> wrote in message
> news:<mailman.109.1078711865.19534.python-list at python.org>...
>> > From: Daniel H Neilson
>> > 
>> > i = 0
>> > for line in file:
>> >     if i == 100:
>> >         action
>> >         i = 0
>> >     else:
>> >         i += 1
>> >     process(line)
> 
>> If you're using Python 2.3.x
>> 
>> for i, line in enumerate(file):
>>     if i % 100 == 0:
>>         action
>> 
>>     process(line)
> 
> Beautiful. Thanks to those who responded. DHN

But be aware that the code falls through so that process(line) executes
every loop whereas it doesn't in your original example.

-- 
Garry Knight
garryknight at gmx.net  ICQ 126351135
Linux registered user 182025



More information about the Python-list mailing list