[Python-Dev] Any reason that any()/all() do not take a predicateargument?
"Martin v. Löwis"
martin at v.loewis.de
Sat Apr 15 22:16:14 CEST 2006
Bill Janssen wrote:
> Yeah, but you can't do more complicated expressions that way, like
>
> any(lambda x: x[3] == "thiskey")
Not /quite/ sure what this is intended to mean, but most likely,
you meant
any(x[3]=="thiskey" for x in seq)
> I think it makes a lot of sense for any and all to take optional
> predicate function arguments.
I don't believe that adds expressiveness: you can always formulate
this with a generator expression - apparently, those are of the
"read-only" nature, i.e. difficult to formulate (assuming you have
no difficulties to read above term).
> I suppose
>
> (len([x for x in SEQ if PRED(x)]) > 0)
>
> will suffice for now. Obvious enough, Martin?
It's simpler written as
any(PRED(x) for x in SEQ)
or
any(True for x in SEQ if PRED(x))
if you want
Using any() has the advantage over len() that the any()
code stops at the first value that becomes true, whereas
the len code ill compute them all.
Regards,
Martin
More information about the Python-Dev
mailing list