I have a list of words / appliances
appliances = ['tv', 'radio', 'oven', 'speaker']
I also have a sentence, which I have tokenized.
sent = ['We have a radio in the Kitchen']
sent1 = word_tokenize[sent]
I want to say that if appliances are in sent1 then print yes else print no. I did the below but keep getting no as the print.
if any(appliances) in sent1:
print ('yes')
else:
print ('no')
Is there a better way of doing this?
Try something like this.
appliances = ['tv', 'radio', 'oven', 'speaker']
sent = ['We have a radio in the Kitchen']
sent1 = list(sent[0].split())
if any([app in sent1 for app in appliances]):
print ('yes')
else:
print ('no')
Use lazy evaluation.
if any(app in sent1 for app in appliances):
print ('yes')
else:
print ('no')
If you want to see with appliance are in your sentence you could do like this.
[app for app in appliances if app in sent1]