Check list of words in another string

PythonList

Python Problem Overview


I can do such thing in python:

l = ['one', 'two', 'three']
if 'some word' in l:
   ...

This will check if 'some word' exists in the list. But can I do reverse thing?

l = ['one', 'two', 'three']
if l in 'some one long two phrase three':
    ...

I have to check whether some words from array are in the string. I can do this using cycle but this way has more lines of code.

Python Solutions


Solution 1 - Python

if any(word in 'some one long two phrase three' for word in list_):

Solution 2 - Python

Here are a couple of alternative ways of doing it, that may be faster or more suitable than KennyTM's answer, depending on the context.

  1. use a regular expression:

    import re words_re = re.compile("|".join(list_of_words))

    if words_re.search('some one long two phrase three'): # do logic you want to perform

  2. You could use sets if you want to match whole words, e.g. you do not want to find the word "the" in the phrase "them theorems are theoretical":

    word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff

Of course you can also do whole word matches with regex using the "\b" token.

The performance of these and Kenny's solution are going to depend on several factors, such as how long the word list and phrase string are, and how often they change. If performance is not an issue then go for the simplest, which is probably Kenny's.

Solution 3 - Python

If your list of words is of substantial length, and you need to do this test many times, it may be worth converting the list to a set and using set intersection to test (with the added benefit that you wil get the actual words that are in both lists):

>>> long_word_list = 'some one long two phrase three about above along after against'
>>> long_word_set = set(long_word_list.split())
>>> set('word along river'.split()) & long_word_set
set(['along'])

Solution 4 - Python

Easiest and Simplest method of solving this problem is using re

import re

search_list = ['one', 'two', 'there']
long_string = 'some one long two phrase three'
if re.compile('|'.join(search_list),re.IGNORECASE).search(long_string): #re.IGNORECASE is used to ignore case
    # Do Something if word is present
else:
    # Do Something else if word is not present

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMax FraiView Question on Stackoverflow
Solution 1 - PythonkennytmView Answer on Stackoverflow
Solution 2 - PythonDave KirbyView Answer on Stackoverflow
Solution 3 - PythonPaulMcGView Answer on Stackoverflow
Solution 4 - PythonAnurag MisraView Answer on Stackoverflow