Check list of words in another string


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.

Asked By: Max Frai



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

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

Answered By: Dave Kirby

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
Answered By: PaulMcG

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 makes the search case-insensitive
    # Do Something if word is present
    # Do Something else if word is not present
Answered By: Anurag Misra
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.