P: n/a

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]
....
1 [(1, 11)]
2 [(2, 12)]
3 [(3, 13)]
4 [(4, 14)]
5 [(5, 15)]
1 [(1, 16)]
2 [(2, 17)]
3 [(3, 18)]
4 [(4, 19)]
5 [(5, 20)]
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  
Share this Question
P: n/a
 tr*******@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 sc***********@acm.org  
P: n/a
 tr*******@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] ... 1 [(1, 11)] 2 [(2, 12)] 3 [(3, 13)] 4 [(4, 14)] 5 [(5, 15)] 1 [(1, 16)] 2 [(2, 17)] 3 [(3, 18)] 4 [(4, 19)] 5 [(5, 20)]
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.
itertools only looks for changes to the key value (the one returned by
operator.itemgetter(0) in your case); it doesn't sort the list for you.
this should work:
for k, g in itertools.groupby(sorted(vals), operator.itemgetter(0)):
print k, [i for i in g]
</F>  
P: n/a

> itertools only looks for changes to the key value (the one returned by operator.itemgetter(0) in your case); it doesn't sort the list for you.
this should work:
for k, g in itertools.groupby(sorted(vals), operator.itemgetter(0)): print k, [i for i in g]
footnote: to turn the contents in an iterator into a list object,
list(g) is a bit more convenient than [i for i in g].
</F>   This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 1336
 replies: 3
 date asked: May 25 '06
