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

Duncan Booth duncan.booth at invalid.invalid
Sat Apr 25 13:11:45 EDT 2009


jazbees <jazbees at gmail.com> wrote:

>>>> 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?

Anyway, it is probably clearer to use the builtin 'any' for code like this:

>>> def hasvowels(s):
...     return any(v in s for v in "aeiou")
...
>>> hasvowels("parsnips")
True
>>> hasvowels("sfwdkj")
False
>>>



>>>> foo = ["green", "orange"]
>>>> bar = ["blue", "green", "red", "yellow"]
>>>> found = lambda x,y:max([z in y for z in x] if x else [False])
>>>> found(foo, bar)
> True
>>>> foo = []
>>>> found(foo, bar)
> False

If you are doing a lot of this consider whether you might be better off 
using sets:

>>> def found(x,y):
...    return bool(set(x).intersection(y))
...



More information about the Python-list mailing list