Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #40868 > unrolled thread

itertools.filterfalse - what is it good for

Started byWolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de>
First post2013-03-08 16:45 +0000
Last post2013-03-09 12:30 -0800
Articles 4 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  itertools.filterfalse - what is it good for Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2013-03-08 16:45 +0000
    Re: itertools.filterfalse - what is it good for Neil Cerutti <neilc@norwich.edu> - 2013-03-08 17:02 +0000
    Re: itertools.filterfalse - what is it good for Miki Tebeka <miki.tebeka@gmail.com> - 2013-03-09 12:30 -0800
    Re: itertools.filterfalse - what is it good for Miki Tebeka <miki.tebeka@gmail.com> - 2013-03-09 12:30 -0800

#40868 — itertools.filterfalse - what is it good for

FromWolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de>
Date2013-03-08 16:45 +0000
Subjectitertools.filterfalse - what is it good for
Message-ID<mailman.3087.1362761126.2939.python-list@python.org>
Dear all,
can anybody point out a situation where you really need itertools.filterfalse() ?
So far, I couldn't think of a case where you couldn't replace it with a
generator expression/if combination.
e.g.,

a=filterfalse(lambda x: x%2, range(1,101))
b=(i for i in range(1,101) if not i % 2)

do not return the same object type, but otherwise are achieving the same thing.
What am I missing here? For sure filterfalse exists for a reason?

Best,
Wolfgang

[toc] | [next] | [standalone]


#40872

FromNeil Cerutti <neilc@norwich.edu>
Date2013-03-08 17:02 +0000
Message-ID<apujtvFjj6gU1@mid.individual.net>
In reply to#40868
On 2013-03-08, Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> wrote:
> Dear all,
> can anybody point out a situation where you really need
> itertools.filterfalse() ? So far, I couldn't think of a case
> where you couldn't replace it with a generator expression/if
> combination. e.g.,
>
> a=filterfalse(lambda x: x%2, range(1,101))
> b=(i for i in range(1,101) if not i % 2)
>
> do not return the same object type, but otherwise are achieving
> the same thing. What am I missing here? For sure filterfalse
> exists for a reason?

It must exist for reasons of convenience and efficiency only.

It can trivially be replaced by filter in all cases (at least in
Python 3), but it saves you from a possibly slow extra function
indirection, and also from needing to define one at all.

-- 
Neil Cerutti

[toc] | [prev] | [next] | [standalone]


#40973

FromMiki Tebeka <miki.tebeka@gmail.com>
Date2013-03-09 12:30 -0800
Message-ID<ac6c2230-1b7a-4e73-a20a-4aa52dafbded@googlegroups.com>
In reply to#40868
> can anybody point out a situation where you really need itertools.filterfalse() ?
Sometimes you get the predicate as a parameter to another function. This way if you want to filter out things you can easily do it. Other language (such as Clojure) have a "complement" function that removes the need of filterfalse.

For example (Python 3):
    def percent_spam(is_spam, documents):
        n_spam = sum(1 for _ in filter(is_spam, documents))
        n_ham = sum(1 for _ in filterfalse(is_spam, documents))
        return float(n_spam) / (n_ham + n_spam)

[toc] | [prev] | [next] | [standalone]


#40974

FromMiki Tebeka <miki.tebeka@gmail.com>
Date2013-03-09 12:30 -0800
Message-ID<mailman.3139.1362861020.2939.python-list@python.org>
In reply to#40868
> can anybody point out a situation where you really need itertools.filterfalse() ?
Sometimes you get the predicate as a parameter to another function. This way if you want to filter out things you can easily do it. Other language (such as Clojure) have a "complement" function that removes the need of filterfalse.

For example (Python 3):
    def percent_spam(is_spam, documents):
        n_spam = sum(1 for _ in filter(is_spam, documents))
        n_ham = sum(1 for _ in filterfalse(is_spam, documents))
        return float(n_spam) / (n_ham + n_spam)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web