Reputation: 744
I would like to check whether 2 words "car" and "motorbike" are in each element of an array in Python. I know how to check for one word with in
but have no idea how to do with 2 words. Really appreciate any help
Upvotes: 3
Views: 26541
Reputation: 232
Use this function to check for two or more keywords in a sentence with 'and' or 'or' operators.
def check_words(operator = 'and', words = ['car', 'bike'],
sentence = 'I own a car but not a bike'):
if operator == 'and':
word_present = True
for w in words:
if w in sentence:
word_present = True
else:
word_present = False
return word_present
elif operator == 'or':
for w in words:
if w in sentence:
return True
else:
return False
check_words(operator = 'and', words = ['car', 'bike'],
sentence = 'I own a car but not a bike')
Upvotes: 0
Reputation: 1211
This link worked for me: It offers 3 solutions. Two methods use list comprehensions and the third one uses map + lambda functions.
I think there is not an easy and pythonic way to do that. You need to use an ugly logic like the next:
image_file_name = 'man_in_car.jpg'
if 'car' in image_file_name and 'man' in image_file_name:
print('"car" and "man" were found in the image_file_name')
That would work for two words, but if you need to check many words, then better use the code in the link above
I would like to be able to do something like:
if 'car' and 'man' in image_file_name:
print('"car" and "man" were found in the image_file_name')
Or:
if any(['car','man'] in image_file_name):
print('"car" and "man" were found in the image_file_name')
But these 2 last pieces of code don't work in python (yet).
Upvotes: 0
Reputation: 1438
I would use the all
function:
wanted_values = ("car", "motorbike")
all(vehicle in text for text in wanted_values)
So if we have a list of strings:
l = ['some car and motorbike',
'a motorbike by a car',
'the car was followed by a motorbike']
lines_with_vehicles = [text for text in l
if all(vehicle in text for text in wanted_values)]
With regex you could do:
# no particular order
car_and_motorbike_pattern = re.compile(r'(car.*motorbike|motorbike.*car)')
all(car_and_motorbike_pattern.search(text) for text in list_of_expressions)
# This works too
car_or_motorbike_pattern = re.compile(r'(car|motorbike)')
get_vehicles = car_or_motorbike_pattern.findall
all(len(set(get_vehicles(text))) == 2 for text in list_of_expressions)
Upvotes: 0
Reputation: 2552
I think a simple solution is this:
all(map(lambda w: w in text, ('car', 'motorbike')))
But there might be a problem with this, depending on how picky you need the comparison to be:
>>> text = 'Can we buy motorbikes in carshops?'
>>> all(map(lambda w: w in text, ('car', 'motorbike')))
True
The words 'car' and 'motorbike' are NOT in the text
, and this still says True
. You might need a full match in words. I would do this:
>>> words = ('car', 'motorbike')
>>> text = 'Can we buy motorbikes in carshops?'
>>> set(words).issubset(text.split())
False
>>> text = 'a car and a motorbike'
>>> set(words).issubset(text.split())
True
And now it works!
Upvotes: 0
Reputation: 364
Use an auxiliar boolean.
car=False
motorbike=False
for elem in array:
if "car" in elem:
car=True
if "motorbike" in elem:
motorbike=True
if car and motorbike:
break
EDIT: I just read "in each element". Just use AND.
Upvotes: 1
Reputation: 626
Two word solution:
for string in array:
if 'car' in string and 'motorbike' in string.split():
print("Car and motorbike are in string")
n-word solution to check if all words in test_words
are in string
:
test_words = ['car', 'motorbike']
contains_all = True
for string in array:
for test_word in test_words:
if test_word not in string.split()::
contains_all = False
break
if not contains_all:
break
if contains_all:
print("All words in each string")
else:
print("Not all words in each string")
Upvotes: 8