How do I process the elements of a sequence in batches, idiomatically?
For example, with the sequence \"abcdef\" and a batch size of 2, I would like to do something
From the docs of more_itertools: more_itertools.chunked()
more_itertools.chunked(iterable, n)
Break an iterable into lists of a given length:
>>> list(chunked([1, 2, 3, 4, 5, 6, 7], 3))
[[1, 2, 3], [4, 5, 6], [7]]
If the length of iterable is not evenly divisible by n, the last returned list will be shorter.
Adapted from this answer for Python 3:
def groupsgen(seq, size):
it = iter(seq)
iterating = True
while iterating:
values = ()
try:
for n in range(size):
values += (next(it),)
except StopIteration:
iterating = False
if not len(values):
return None
yield values
It will safely terminate and won't discard values if their number is not divisible by size
.
One solution, although I challenge someone to do better ;-)
a = 'abcdef'
b = [[a[i-1], a[i]] for i in range(1, len(a), 2)]
for x, y in b:
print "%s%s\n" % (x, y)
Don't forget about the zip() function:
a = 'abcdef'
for x,y in zip(a[::2], a[1::2]):
print '%s%s' % (x,y)
How about itertools?
from itertools import islice, groupby
def chunks_islice(seq, size):
while True:
aux = list(islice(seq, 0, size))
if not aux: break
yield "".join(aux)
def chunks_groupby(seq, size):
for k, chunk in groupby(enumerate(seq), lambda x: x[0] / size):
yield "".join([i[1] for i in chunk])