New submission from Jiba jibal...@free.fr:
In some situation, itertools.groupby fails to group the objects, and produces
several groups with the same key. For example, the following code :
from itertools import *
class P(object):
def __init__(self, key):
self.key = key
p1 = P(1)
p2 =
Petri Lehtinen pe...@digip.org added the comment:
groupby() changes the group when the key changes in the input it iterates. If
you want to have p1 and p3 to go to the same group, you need to sort the input
by P.key first.
This is clearly documented, too:
The operation of groupby() is
Jiba jibal...@free.fr added the comment:
Ok, I understand.
However, in my initial problem, the sequence passed to groupby was a set, e.g.
(modifying my previous example) :
groupby(set([p1, p2, p3]), lambda p: p.key)
If I understand well how groupby() works, the result of a groupby
Petri Lehtinen pe...@digip.org added the comment:
You're right, the result over a set would be unpredictable.
The point of the itertools module is to be able to a) cope with massive amounts
of data and b) be a set of tools instead of complete solutions for all problems.
Because of both of the