Using Python to Check If List of Words in String

To check if a list of words is in a string using Python, the easiest way is with list comprehension.

>>> list_of_words = ["this","words","string"]
>>> string = "this is a string with words."
>>> print([word in string for word in list_of_words])
[True, True, True]
>>>

If you want to check if all words of a list are in a string, you can use list comprehension and the Python all() function.

>>> list_of_words = ["this","words","string"]
>>> string = "this is a string with words."
>>> print(all([word in string for word in list_of_words]))
True
>>> 

 If you want to check if any of the words of a list are in a string, then you can use the Python any() function.

>>> list_of_words = ["this","words","string"]
>>> string = "this is a string with words."
>>> print(any([word in string for word in list_of_words]))
True
>>> 

When working with strings in Python, the ability to be able to get information about them is valuable.

One such piece of information is if certain words are in a string variable.

You can check if word is a in a string with the Python in operator, but if you want to check if a list of words is in a string, then you have to do a little more.

To check if a list of words is in a string using Python, the easiest way is with list comprehension.

Below are some examples showing you how to use list comprehension to check if more than one word is in a string using Python.

>>> list_of_words = ["this","words","string"]
>>> another_list_of_words = ["a","is","apple"]
>>> string = "this is a string with words."
>>> print([word in string for word in list_of_words])
[True, True, True]
>>> print([word in string for word in another_list_of_words])
[True, True, False]
>>> 

Checking if All Words in List are in String Using Python

If you want to check if all of the words are in a string using Python, you can use the Python all() function.

all() checks if all of the items of an iterable are True.

If all of the items in an iterable is True, then any() returns True.

Below is an example of how to check if all words in a list of words are in a string variable in Python.

>>> list_of_words = ["this","words","string"]
>>> string = "this is a string with words."
>>> print(all([word in string for word in list_of_words]))
True
>>> 

Checking if Any Words in List are in String Using Python

If you want to check if any of the words are in a string using Python, you can use the Python any() function.

any() checks if any of the items of an iterable are True.

If at least one item in an iterable is True, then any() returns True.

Below is an example of how to check if any words in a list of words are in a string variable in Python.

>>> list_of_words = ["this","words","string"]
>>> string = "this is a string with words."
>>> print(any([word in string for word in list_of_words]))
True
>>> 

 

 

>>> list_of_words = ["this","words","string"] --> 檢查的項

>>> string = "this is a string with words." --> 被檢測的字符串

word in string for word in list_of_words --> 需要檢查的項是否在字符串中

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章