Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If I have a list of words, how can I check if string does not contain any of the words in the list, and efficiently?

As title says, I have a list of words, Like stopWords = ["the", "and", "with", etc...] and I'm receiving text like "Kill the fox and dog". I want the output like "Kill fox dog" very efficiently and fast. How can I do this (I know I can iterate using a for loop, but thats not very efficient)

like image 330
Thor Correia Avatar asked Jun 14 '12 02:06

Thor Correia


People also ask

How do you check if a list of string contains a string?

Using String.contains() method for each substring. You can terminate the loop on the first match of the substring, or create a utility function that returns true if the specified string contains any of the substrings from the specified list.

How do you check if a string contains a set of words?

You can use the PHP strpos() function to check whether a string contains a specific word or not. The strpos() function returns the position of the first occurrence of a substring in a string. If the substring is not found it returns false . Also note that string positions start at 0, and not 1.

How do you check if a list does not contain a string Python?

The easiest way to check if a Python string contains a substring is to use the in operator. The in operator is used to check data structures for membership in Python. It returns a Boolean (either True or False ).


1 Answers

The most imporant improvement is to make stopWords a set. This means the lookups will be very fast

stopWords = set(["the", "and", "with", etc...])
" ".join(word for word in msg.split() if word not in stopWords)

If you just want to know if any of the stopWords are in the text

if any(word in stopWords for word in msg.split()):
    ...
like image 84
John La Rooy Avatar answered Nov 15 '22 09:11

John La Rooy