jeudi 26 septembre 2019

Python Most efficient If statement that matches words in a compare string to a keyword string

I am looking for the most efficient (fastest) way to create an if statement that looks for individual words in a keyword string. e.g.

I don't mind if this is done through regular expressions

keywords = 'dog wolf lion shark large whale'
stringToCompare = 'big dog'

if stringToCompare in keywords:
     #dosomething if any word in the compare string matches the keywords

The expected results based on the following input would be:

keywords = 'dog wolf lion shark large whale'

stringToCompare || Expected Result

'big dog' || True

'big cat' || False

'cat shark' || True

'catshark' || False

Aucun commentaire:

Enregistrer un commentaire