[Python-ideas] Change how Generator Expressions handle StopIteration

Ron Adam ron3200 at gmail.com
Tue Nov 4 04:50:38 CET 2014


Just a very minor correction, list(group(itr) instead of list comp. ...


On 11/03/2014 09:17 PM, Ron Adam wrote:
> def group_by(iterable):
>      """ Yield (key, (group))'s of like values
>          from an iterator.
>      """
>      itr = PeekIter(iterable)
>
>      def group(it):
>          # Yield only like values.
>          k = it.peek()
>          while it.peek() == k:
>              yield next(it)
>
>      while itr.ok():
>          yield itr.peek(), [x for x in group(itr)]


def group_by(iterable):
     """ Yield (key, (group))'s of like values
         from an iterator.
     """
     def group(it):
         # Yield only like values.
         k = it.peek()
         while it.peek() == k:
             yield next(it)

     itr = PeekIter(iterable)
     while itr.ok():
         yield itr.peek(), list(group(itr))

Cheers,
    Ron



More information about the Python-ideas mailing list