Created
April 29, 2015 15:42
-
-
Save jmg/669a7d42801aae97bbff to your computer and use it in GitHub Desktop.
slice objects in sublists sharing a common propery
This file contains hidden or 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 slice(objects, prop): | |
| """ | |
| slice objects in sublists sharing a common propery | |
| """ | |
| if not objects: | |
| return [] | |
| def get_prop(obj, prop): | |
| if not isinstance(objects[0], dict): | |
| return obj.__dict__[prop] | |
| return obj[prop] | |
| set_objects = set([get_prop(o, prop) for o in objects]) | |
| return [[o for o in objects if get_prop(o, prop) == obj] for obj in set_objects] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment