Skip to content

Instantly share code, notes, and snippets.

@masak
Created July 31, 2013 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masak/6122083 to your computer and use it in GitHub Desktop.
Save masak/6122083 to your computer and use it in GitHub Desktop.
A classify of my own
>>> oddness = lambda n: n % 2
>>> oddness(0)
0
>>> oddness(3)
1
>>> def classify(things, func):
... result = {}
... for t in things:
... if func(t) in result:
... result[func(t)].append(t)
... else:
... result[func(t)] = [t]
... return result
...
>>> classify([1,2,3,4,5,6,7,8,9], oddness)
{0: [2, 4, 6, 8], 1: [1, 3, 5, 7, 9]}
>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment