435,301 Members | 1,741 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,301 IT Pros & Developers. It's quick & easy.

Problem with itertools.groupby.

 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 May 25 '06 #1
3 Replies

 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 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 May 25 '06 #2

 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] May 25 '06 #3

 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]. May 25 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion. 