Created
August 1, 2012 22:20
-
-
Save kurtbrose/3231223 to your computer and use it in GitHub Desktop.
python split and isplit
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 split(pred, array): | |
| return filter(pred, array), filter(lambda e: not pred(e), array) | |
| import itertools | |
| def isplit(pred, iterable): | |
| pos, neg = itertools.tee(iterable, 2) | |
| return itertools.ifilter(pred, pos), itertools.ifilterfalse(pred, neg) | |
| ''' | |
| >>> split(lambda e: e == 0, [1,2,0,3,0,4]) | |
| ([0, 0], [1, 2, 3, 4]) | |
| This seems like it may be a generally useful little function; is there a cleaner/better way to do it? | |
| ''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment