How do you check if a word in a list is in a string 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.

We are given a String and our task is to test if the string contains elements from the list.

Example:

Input: String: Geeks for Geeks is one of the best company. List: ['Geeks', 'for'] Output: Does string contain any list element : True

Naive Approach checking each word in the string

Here we are splitting the string into list of words and then matching each word of this list with the already present list of words we want to check.

Python3

test_string = "There are 2 apples for 4 persons"

test_list = ['apples', 'oranges']

print("The original string : " + test_string)

print("The original list : " + str(test_list))

test_string=test_string.split(" ")

flag=0

for i in test_string:

    for j in test_list:

        if i==j:

            flag=1

            break

if flag==1:

    print("String contains the list element")

else:

    print("String does not contains the list element")

Output:

The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element

Using list comprehension to check if string contains element from list

This problem can be solved using the list comprehension, in this, we check for the list and also with string elements if we can find a match, and return true, if we find one and false is not using the conditional statements.
 

Python3

test_string = "There are 2 apples for 4 persons"

test_list = ['apples', 'oranges']

print("The original string : " + test_string)

print("The original list : " + str(test_list))

res = [ele for ele in test_list if(ele in test_string)]

print("Does string contain any list element : " + str(bool(res)))

Output:

The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True

Using any() to check if string contains element from list

Using any function is the most classical way in which you can perform this task and also efficiently. This function checks for match in string with match of each element of list.
 

Python3

test_string = "There are 2 apples for 4 persons"

test_list = ['apples', 'oranges']

print("The original string : " + test_string)

print("The original list : " + str(test_list))

res = any(ele in test_string for ele in test_list)

print("Does string contain any list element : " + str(res))

Output:

The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True

Using find() method to check if string contains element from list

Here we are using the find() method to check the occurrence of the word and it returns -1 if the word does not exist in the list.

Python3

test_string = "There are 2 apples for 4 persons"

test_list = ['apples', 'oranges']

print("The original string : " + test_string)

print("The original list : " + str(test_list))

res=False

c=0

for i in test_list:

    if(test_string.find(i)!=-1):

        c+=1

if(c>=1):

    res=True

print("Does string contain any list element : " + str(bool(res)))

Output:

The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True

How do you check if a certain word is in a string Python?

The simplest way to check if a string contains a substring in Python is to use the in operator. This will return True or False depending on whether the substring is found. For example: sentence = 'There are more trees on Earth than stars in the Milky Way galaxy' word = 'galaxy' if word in sentence: print('Word found.

How do you check if items of a list are in a string?

Using any() to check if string contains element from list. Using any function is the most classical way in which you can perform this task and also efficiently. This function checks for match in string with match of each element of list.

How do you check if a specific word is in a string?

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 word exists in a list?

Method 2: Check if an element exists in the list using count() We can use the in-built python List method, count(), to check if the passed element exists in the List. If the passed element exists in the List, the count() method will show the number of times it occurs in the entire list.

Chủ đề