[Python-ideas] Batching/grouping function for itertools

Mathias Panzenböck grosser.meister.morti at gmx.net
Sun Dec 8 23:26:21 CET 2013


I see. Well, I woudln't expect that behaviour from such a function.

On 12/08/2013 07:06 PM, Amber Yust wrote:
> So does zip if the items are of unequal length, and the two code examples I provided (the one using zip and the
> long-form one) are equivalent.
>
> On Sun Dec 08 2013 at 9:49:56 AM, Mathias Panzenböck <grosser.meister.morti at gmx.net
> <mailto:grosser.meister.morti at gmx.net>> wrote:
>
>     On 12/08/2013 05:44 AM, 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.
>      >
>      > Existing possible solutions include both the "clever" but somewhat unreadable...
>      >
>      >      batched_iter = zip(*[iter(input_iter)]*n)
>      >
>      > ...and the long-form...
>      >
>      >      def batch(input_iter, n):
>      >          input_iter = iter(input_iter)
>      >          while True:
>      >              yield [input_iter.next() for _ in range(n)]
>      >
>
>     This function drops items if the length of the input sequence is not a multiple of n. Fix:
>
>              def batch(it, n):
>                      it = iter(it)
>                      while True:
>                              slice = []
>                              for _ in range(n):
>                                      try:
>                                              slice.append(it.next())
>                                      except StopIteration:
>                                              if slice:
>                                                      yield slice
>                                              return
>                              yield slice
>
>      > There doesn't seem, however, to be one clear "right" way to do this. Every time I come up against this task, I go
>     back
>      > to itertools expecting one of the grouping functions there to cover it, but they don't.
>      >
>      > It seems like it would be a natural fit for itertools, and it would simplify things like processing of file
>     formats that
>      > use a consistent number of lines per entry, et cetera.
>      >
>      > ~Amber
>      >
>
>     _________________________________________________
>     Python-ideas mailing list
>     Python-ideas at python.org <mailto:Python-ideas at python.org>
>     https://mail.python.org/__mailman/listinfo/python-ideas <https://mail.python.org/mailman/listinfo/python-ideas>
>     Code of Conduct: http://python.org/psf/__codeofconduct/ <http://python.org/psf/codeofconduct/>
>



More information about the Python-ideas mailing list