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 --> 需要检查的项是否在字符串中

posted @   MoKin_Li  阅读(23)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 2 本地部署DeepSeek模型构建本地知识库+联网搜索详细步骤
点击右上角即可分享
微信分享提示