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]]
--
http://mail.python.org/mailman/listinfo/python-list