0

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?

smac89
  • 39,374
  • 15
  • 132
  • 179
ben121
  • 879
  • 1
  • 11
  • 23

1 Answers1

3

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')

Edit based on @tobias_k comments

Use lazy evaluation.

if any(app in sent1 for app in appliances):
    print ('yes')
else:
    print ('no')

Edit : based on @ben121 comment

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]
Florian Bernard
  • 2,561
  • 1
  • 9
  • 22