How to iterate the input over a particular size?

Peter Otten __peter__ at web.de
Mon Dec 28 13:10:29 EST 2009


Lie Ryan wrote:

> On 12/28/2009 8:54 PM, Peter Otten wrote:
>> Francesco Bochicchio wrote:
>> One with lazy chunks:
>>
>>>>> from itertools import chain, islice
>>>>> def chunks(items, n):
>> ....     items = iter(items)
>> ....     for first in items:
>> ....             yield chain((first,), islice(items, n-1))
>> ....
>>>>> [list(chunk) for chunk in chunks(range(10), 3)]
>> [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]
> 
> 
> better:
> import itertools
> def chunks(items, n):
>      ctr = (x // n for x in itertools.count())
>      return itertools.groupby(items, lambda _: next(ctr))
> 
> input_list = range(10)
> list(list(x[1]) for x  in chunks(input_list, 3))
> # [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]

I disagree. It may be of little practical relevance that this fails after 
about sys.maxint items (Python 2.5) or at least slows down (Python 2.6) when 
Python long integer arithmetic kicks in, but I still feel uneasy about it. 
If I were to use groupby() I'd probably code the "counter" as

ctr = cycle([False]*n + [True]*n)

Peter



More information about the Python-list mailing list