Skip to content

Instantly share code, notes, and snippets.

@Jim-Holmstroem
Created August 2, 2012 13:06
Show Gist options
  • Save Jim-Holmstroem/3236929 to your computer and use it in GitHub Desktop.
Save Jim-Holmstroem/3236929 to your computer and use it in GitHub Desktop.
python groupby unsorted and loose the iterators
import itertools as it
def sortandgroupby(data, **kwargs):
return dict(map(lambda(k,v):(k,list(v)),it.groupby(sorted(data,kwargs),kwargs)))
print sortandgroupby(range(30),lambda x:mod(x,3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment