filter iterable based on predicate take from another iterable

Discussion in 'Python' started by bernhard.voigt@gmail.com, Dec 10, 2008.

  1. Guest

    Hi,

    is there is a neat way to select items from an iterable based on
    predicates stored in another iterable without zipping? I can do
    something like this:

    import itertools
    foo = range(10)
    # select even numbers
    bar = map(lambda i: i%2, foo)
    foobarselected = itertools.ifilterfalse(lambda t: t[0], itertools.izip
    (bar,foo))
    # for simplicity I want to work with the single item list, not the
    zipped one
    fooselected = list(t[1] for t in foobarselected)

    However, it would be nice to have a function combining the last two
    instructions. Something like
    itertools.ifilterother(bar, foo) -> yield iterator with items from foo
    where bar is true

    Thanks! Bernhard
    , Dec 10, 2008
    #1
    1. Advertising

  2. Peter Otten Guest

    wrote:

    > is there is a neat way to select items from an iterable based on
    > predicates stored in another iterable without zipping? I can do
    > something like this:
    >
    > import itertools
    > foo = range(10)
    > # select even numbers
    > bar = map(lambda i: i%2, foo)
    > foobarselected = itertools.ifilterfalse(lambda t: t[0], itertools.izip
    > (bar,foo))
    > # for simplicity I want to work with the single item list, not the
    > zipped one
    > fooselected = list(t[1] for t in foobarselected)
    >
    > However, it would be nice to have a function combining the last two
    > instructions. Something like
    > itertools.ifilterother(bar, foo) -> yield iterator with items from foo
    > where bar is true


    I think it's a good approach to keep the number of primitives low. I find
    the list comprehension combined with izip() quite readable:

    [v for f, v in izip(bar, foo) if not f(v)]

    Peter
    Peter Otten, Dec 10, 2008
    #2
    1. Advertising

  3. James Stroud Guest

    Peter Otten wrote:
    > wrote:
    >
    >> is there is a neat way to select items from an iterable based on
    >> predicates stored in another iterable without zipping? I can do
    >> something like this:
    >>
    >> import itertools
    >> foo = range(10)
    >> # select even numbers
    >> bar = map(lambda i: i%2, foo)
    >> foobarselected = itertools.ifilterfalse(lambda t: t[0], itertools.izip
    >> (bar,foo))
    >> # for simplicity I want to work with the single item list, not the
    >> zipped one
    >> fooselected = list(t[1] for t in foobarselected)
    >>
    >> However, it would be nice to have a function combining the last two
    >> instructions. Something like
    >> itertools.ifilterother(bar, foo) -> yield iterator with items from foo
    >> where bar is true

    >
    > I think it's a good approach to keep the number of primitives low. I find
    > the list comprehension combined with izip() quite readable:
    >
    > [v for f, v in izip(bar, foo) if not f(v)]
    >
    > Peter


    If you want an iterator without requiring making a list, you can simply
    use parentheses instead of brackets:

    agenerator = (v for f, v in izip(bar, foo) if not f(v))

    This is perfectly lazy but not immune to problems when foo or bar is
    changed before the generator is fully consumed.

    James



    --
    James Stroud
    UCLA-DOE Institute for Genomics and Proteomics
    Box 951570
    Los Angeles, CA 90095

    http://www.jamesstroud.com
    James Stroud, Dec 10, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. andy lim
    Replies:
    9
    Views:
    390
    Lucas Tam
    Apr 17, 2005
  2. Peter Olcott
    Replies:
    5
    Views:
    376
    Peter Olcott
    Dec 6, 2005
  3. zax75
    Replies:
    1
    Views:
    1,095
  4. Rajanikanth Jammalamadaka

    split a list based on a predicate

    Rajanikanth Jammalamadaka, Oct 9, 2008, in forum: Python
    Replies:
    3
    Views:
    244
    Rajanikanth Jammalamadaka
    Oct 9, 2008
  5. Tom Anderson
    Replies:
    6
    Views:
    415
    Tom Anderson
    Jun 25, 2010
Loading...

Share This Page