SohrabFarjami
SohrabFarjami

Reputation: 19

How to search for specific set of keyword in string python

list = ['hi', 'hello', 'pee', ('poo' , 'pap') , 'noog']

post_lower = ' i am a big idiot'
if any(word in post_lower for word in list):
    print('true')
else:
    print('false')

what im trying to do is if there is hi or hello it will return true, however i want it to only return true if BOTH poo and pap is in the string. I am getting TypeError: 'in ' requires string as left operand, not tuple

the reason im making this because im making a bot that recognizes certain keywords in posts, like for example python course and beginner. Now python on it self does not mean anything however if both python course and beginner is in a string it means that the post is asking for best courses for beginners. However it can be worded differently and have different grammar

thanks for your help

Upvotes: 0

Views: 78

Answers (1)

itogaston
itogaston

Reputation: 399

list = ['hi', 'hello', 'pee', ('poo' , 'pap') , 'noog']

post_lower = ' i am a big idiot'
for item in list:
    if type(item) == str and item in post_lower:
        print('true')
    elif type(item) == tuple and all(word in post_lower for word in item):
        print('true')
    else:
        print('false')
   

Upvotes: 1

Related Questions