enumerated while loop

Jan Kaliszewski zuo at chopin.edu.pl
Sat Jan 23 10:04:52 EST 2010


23-01-2010 o 15:49:23 Diez B. Roggisch <deets at nospam.web.de> wrote:

> Am 23.01.10 15:44, schrieb Roald de Vries:
>> Dear all,
>>
>> I sometimes want to use an infinite while loop with access to the loop
>> index, like this:
>>
>> def naturals():
>> i = 0
>> while True:
>> yield i
>> y += 1
>>
>> for i in naturals():
>> print(i)
>>
>> I assume a function like 'naturals' already exists, or a similar
>> construction for the same purpose. But what is it called?

itertools.count() -- see  
http://docs.python.org/library/itertools.html#itertools.count

> for i, item in enumerate(iterable):
>      ....

That's completely beside the point.

Regards,
*j



More information about the Python-list mailing list