Problem with itertools.groupby.

Scott David Daniels scott.daniels at acm.org
Thu May 25 17:28:24 EDT 2006


trebucket at gmail.com wrote:
> What am I doing wrong here?
> 
>>>> import operator
>>>> import itertools
>>>> vals = [(1, 11), (2, 12), (3, 13), (4, 14), (5, 15),
> ...      (1, 16), (2, 17), (3, 18), (4, 19), (5, 20)]
>>>> for k, g in itertools.groupby(iter(vals), operator.itemgetter(0)):
> ...     print k, [i for i in g]
> ...
> 
> What I want is tuples starting with identical numbers to be grouped.  I
> cannot figure out why this is not working.  If anyone has any insights,
> I would appreciate it.
> 
> - Alex Ross
> 
Sort the list before using it.

 >>> vals = [(1, 11), (2, 12), (3, 13), (4, 14), (5, 15),
             (1, 16), (2, 17), (3, 18), (4, 19), (5, 20)]
 >>> def first(pair):
         return pair[0]
 >>> for k, g in itertools.groupby(sorted(vals, key=first), first):
         print k, [i for i in g]

"groupby" depends on the source stream having the clustering you need.
Otherwise it could not work "on the fly" for arbitrarily large sources.
Often you can arrange for your data source to be clustered; when you
cannot, the groupby arg is a great sort key.

--Scott David Daniels
scott.daniels at acm.org



More information about the Python-list mailing list