Skip to content

Instantly share code, notes, and snippets.

@kurtbrose
Created August 1, 2012 22:20
Show Gist options
  • Select an option

  • Save kurtbrose/3231223 to your computer and use it in GitHub Desktop.

Select an option

Save kurtbrose/3231223 to your computer and use it in GitHub Desktop.
python split and isplit
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