filter iterable based on predicate take from another iterable

B

bernhard.voigt

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
 
P

Peter Otten

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
 
J

James Stroud

Peter said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top