Problem with itertools.groupby.

T

trebucket

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
 
S

Scott David Daniels

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
(e-mail address removed)
 
F

Fredrik Lundh

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>
 
F

Fredrik Lundh

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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,233
Latest member
AlyssaCrai

Latest Threads

Top