Created
July 19, 2010 14:37
-
-
Save narma/481485 to your computer and use it in GitHub Desktop.
Real groupby of python itertools
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def realgroupby(it, key): | |
from itertools import groupby, chain | |
d = {} | |
for k, ii in groupby(it, key): | |
if k in d: | |
d[k] = chain(d[k], list(ii)) | |
else: | |
d[k] = list(ii) | |
return d |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment