[Python-ideas] Batching/grouping function for itertools
Steven D'Aprano
steve at pearwood.info
Sun Dec 8 13:16:29 CET 2013
On Sun, Dec 08, 2013 at 01:30:56PM +0200, Serhiy Storchaka wrote:
> 08.12.13 11:25, Steven D'Aprano написав(ла):
> >In the second case, there is a question about what to do with sequences
> >that are not a multiple of the window size. Similar to zip(), there are
> >two things one might do:
> >
> >- pad with some given object;
> >- raise an exception
>
> 3) emit last chunk incompleted;
Given a window size of two, and input data [a, b, c], are you suggesting
a variety that returns this?
(a,b), (c,)
There is no need for a separate function for that. Given a version that
takes a pad object, if the pad argument is not given, return a partial
chunk at the end.
> 4) skip incomplete chunk.
The very next sentence in my post references that:
"If you want to just ignore extra items, just catch the exception and
continue."
There is no need for an extra function covering that case.
> There is also a question about result's type. Sometimes you need an
> iterator of subsequences (i.e. split string on equal string chunks),
> sometimes an iterator of iterators is enough.
None of the other itertools functions treat strings specially. Why
should this one? If you want to re-join them into strings, you can do so
with a trivial wrapper:
(''.join(elements) for elements in group("some string", 3, pad=' '))
ought to do the trick, assuming group returns tuples or lists of
characters.
Re-combining the iterated-upon elements into the input type is not the
responsibility of itertools.
--
Steven
More information about the Python-ideas
mailing list