[False,True] and [True,True] --> [True, True]?????

A

Arnaud Delobelle

John Posner said:
jazbees said:
I'm surprised to see that the use of min and max for element-wise
comparison with lists has not been mentioned. When fed lists of True/
False values, max will return True if there is at least one True in
the list, while min will return False if there is at least one False.
Going back to the OP's initial example, one could wrap a min check on
each list inside another min.

I agree with Duncan Booth that any() is equivalent to -- and better
than -- max() for handling boolean values. But what boolean-oriented
function is equivalent to min()? How about this, which returns the
negation of the min() result:

def at_least_one_false(value_list):
"""
return True if at least one value in value_list is logically FALSE
"""
return any( [not val for val in value_list] )

This works, but the short-circuit feature of any() is undermined by
the process-the-whole-sequence behavior of the list comprehension. So,
let's delete the square brackets, converting the list comprehension
into a generator expression:

return any( not val for val in value_list )

This is the same as:

return not all(value_list)
According to timeit.Timer.timeit(), this change improves performance
from 14.59 seconds to 10.49 seconds, with a long value_list:

[True]*20 + [False] + [True]*30

But the generator expression produces worse performance with a shorter
value_list:

[True]*2 + [False] + [True]*3
 
J

jazbees

jazbees said:
hasvowels = lambda x:max([y in x for y in "aeiou"])
hasvowels("parsnips") True
hasvowels("sfwdkj")
False

Do you object to using def to define functions?

Not at all. Do you object to my use of lambdas? I'm not aware of
anything that says it's bad form to define a function using a lambda
when the only thing that a function does is immediately return some
calculated value.
Anyway, it is probably clearer to use the builtin 'any' for code like this:


...     return any(v in s for v in "aeiou")

I wasn't aware of either "any" or "all". Thanks for the info!
Unfortunately this recent project where I used "min" and "max" is
running on a system using Python 2.4, so "any" and "all" are not
available.
If you are doing a lot of this consider whether you might be better off
using sets:


...    return bool(set(x).intersection(y))

I haven't used sets very much, but I'll definitely keep this in mind.
Thanks!
 
J

John Machin

jazbees said:
hasvowels = lambda x:max([y in x for y in "aeiou"])
hasvowels("parsnips")
True
hasvowels("sfwdkj")
False
Do you object to using def to define functions?

Not at all.  Do you object to my use of lambdas?  I'm not aware of
anything that says it's bad form to define a function using a lambda
when the only thing that a function does is immediately return some
calculated value.
Anyway, it is probably clearer to use the builtin 'any' for code like this:
...     return any(v in s for v in "aeiou")

I wasn't aware of either "any" or "all".  Thanks for the info!
Unfortunately this recent project where I used "min" and "max" is
running on a system using Python 2.4, so "any" and "all" are not
available.
If you are doing a lot of this consider whether you might be better off
using sets:
...    return bool(set(x).intersection(y))

I haven't used sets very much, but I'll definitely keep this in mind.

Something else worth noting:

dos-prompt>\python24\python -mtimeit -s"hasvowels=lambda x:max([y in x
for y in 'aeiou'])" "hasvowels('parsnips')"
100000 loops, best of 3: 3.08 usec per loop

dos-prompt>\python24\python -mtimeit -s"hasvowels=lambda x:max([y in x
for y in 'aeiou'])" "hasvowels('qwrtypsdf')"
100000 loops, best of 3: 3.06 usec per loop

dos-prompt>\python24\python -mtimeit -s"import re; hasvowels=re.compile
('[aeiou]').search" "hasvowels('parsnips')"
1000000 loops, best of 3: 1.32 usec per loop

dos-prompt>\python24\python -mtimeit -s"import re; hasvowels=re.compile
('[aeiou]').search" "hasvowels('qwrtypsdf')"
1000000 loops, best of 3: 0.934 usec per loop

HTH,
John
 
T

Terry Reedy

jazbees said:
jazbees said:
hasvowels = lambda x:max([y in x for y in "aeiou"])
hasvowels("parsnips")
True
hasvowels("sfwdkj")
False
Do you object to using def to define functions?

Not at all. Do you object to my use of lambdas? I'm not aware of
anything that says it's bad form to define a function using a lambda
when the only thing that a function does is immediately return some
calculated value.

The difference between

hasvowels = lambda x:max([y in x for y in "aeiou"])

and

def hasvowels(x): return max([y in x for y in "aeiou"])

is that the first is 4 chars shorter, but the result has a generic
..__name__ attribute of '<lambda>' insteand of the specific 'hasvowels',
which is definitely more useful. Given this and the that the main
purpose of lambda is to avoid a local name binding, many consider its
use in 'name = lambda...' to be bad form.

tjr
 
J

jazbees

The difference between

hasvowels = lambda x:max([y in x for y in "aeiou"])

and

def hasvowels(x): return max([y in x for y in "aeiou"])

is that the first is 4 chars shorter, but the result has a generic
.__name__ attribute of '<lambda>' insteand of the specific 'hasvowels',
which is definitely more useful.  Given this and the that the main
purpose of lambda is to avoid a local name binding, many consider its
use in 'name = lambda...' to be bad form.

tjr

Point taken. Thanks for the explanation, Terry! Thanks also to John
for pointing out the execution speed difference when compared to
regular expressions. I try as much as possible to experiment with
different variations for certain code blocks, but I still don't have
enough of the language in my head to think of all the possible
alternatives. Those examples are great for oiling the mental
machine. Cheers!

Justin
 

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

No members online now.

Forum statistics

Threads
474,292
Messages
2,571,494
Members
48,171
Latest member
EllaHolmwo

Latest Threads

Top