[Python-ideas] Batching/grouping function for itertools
yoav glazner
yoavglazner at gmail.com
Sun Dec 8 19:57:32 CET 2013
Hi
On Sun, Dec 8, 2013 at 8:24 PM, Mark Lawrence <breamoreboy at yahoo.co.uk>wrote:
> On 08/12/2013 04:44, Amber Yust wrote:
>
>> After seeing yet another person asking how to do this on #python (and
>> having needed to do it in the past myself), I'm wondering why itertools
>> doesn't have a function to break an iterator up into N-sized chunks.
>>
>>
> As discussed umpteen times previously, there is no way that we can agree
> on "a function" that can meet all of the variations that have been proposed
> on this theme.
>
>
Maybe if we add this function:
>>> def mod_pad(it, modulo, fillval):
'"".join(mod_pad("hello", 3, fillval="!")) => hello!'
for i, val in enumerate(iter(it)):
yield val
for _ in range(i%modulo):
yield fillval
>>> "".join(mod_pad("hello", 3, fillval="!"))
'hello!'
Then we can make grouper/batcher throw an exception in the case of iter_len
% modulo != 0
grouper(mod_pad("hello", 3, fillval="!"), 3) => hel, lo! (in a iterator...)
grouper('hello', 3) => BOOM
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20131208/1164efbe/attachment.html>
More information about the Python-ideas
mailing list