Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > filter iterable based on predicate take from another iterable

Reply
Thread Tools

filter iterable based on predicate take from another iterable

 
 
bernhard.voigt@gmail.com
Guest
Posts: n/a
 
      12-10-2008
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
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      12-10-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
James Stroud
Guest
Posts: n/a
 
      12-10-2008
Peter Otten wrote:
> (E-Mail Removed) 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a 'Filter' or 'Predicate' interface anywhere in the JDK? Tom Anderson Java 6 06-25-2010 06:20 PM
split a list based on a predicate Rajanikanth Jammalamadaka Python 3 10-09-2008 05:01 PM
get position after spec. predicate awebguynow XML 1 01-27-2006 08:42 AM
Binary predicate member function needs access to another object's data Peter Olcott C++ 5 12-06-2005 03:54 AM
Predicate by attribute? Jacques XML 2 06-24-2004 11:45 AM



Advertisments