All (generator) returns True when it should return False
I want to check if a string contains all the keywords. I am using Enthought Canopy .
For example:
string = 'I like roses but not violets'
key_words = ['roses', 'violets', 'tulips']
I read that the function all
will serve me well. When I use this function like this
if all( keys in string.lower().split() for keys in key_words):
print True
Then it comes back True
.
I expect to False
be returned since it is tulips
not in string.lower().split()
.
How can I fix this?
source to share
Probably there is in your code from numpy import *
. numpy
all
the method doesn't handle generators just fine.
[1]: string = 'I like roses but not violets'
[2]: key_words = ['roses', 'violets', 'tulips']
[3]: if all( keys in string.lower().split() for keys in key_words):
...: print True
...:
[4]: from numpy import *
[5]: if all( keys in string.lower().split() for keys in key_words):
print True
...:
True
If the context is out of your control, you can use from __builtin__ import all
to revert all
to your default version in your file. However, it is recommended to use either selective or qualified imports numpy
.
source to share