[issue14828] itertools.groupby not working as expected

Petri Lehtinen report at bugs.python.org
Wed May 16 12:23:07 CEST 2012


Petri Lehtinen <petri at 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 similar to the uniq filter in Unix.
    It generates a break or new group every time the value of the key 
    function changes (which is why it is usually necessary to have 
    sorted the data using the same key function). That behavior differs
    from SQL’s GROUP BY which aggregates common elements regardless of
    their input order.

----------
nosy: +petri.lehtinen
resolution:  -> invalid
status: open -> closed

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue14828>
_______________________________________


More information about the Python-bugs-list mailing list