Sequence splitting

Paul Rubin http
Thu Jul 2 23:14:24 EDT 2009


schickb <schickb at gmail.com> writes:
> def split(seq, func=None):
>     if func is None:
>         func = bool
>     t, f = [], []
>     for item in seq:
>         if func(item):
>             t.append(item)
>         else:
>             f.append(item)
>     return (t, f)

untested:

   def split(seq, func=bool):
      xs = zip(seq, itertools.imap(func, seq))
      t = list(x for (x,y) in xs if y)
      f = list(x for (x,y) in xs if not y)
      return (t, f)



More information about the Python-list mailing list