Python中如何判断列表中的元素,是否在一段文本中??

python 复制代码
#我的Python教程
#官方微信公众号:wdPython

1.要判断列表中的每个元素是否在一段文本中 ,可以使用Python中的字符串的 in 运算符来实现。以下是一个示例代码:

python 复制代码
text = "Hello, how are you today?"
word_list = ["Hello", "are", "you", "today", "Python"]

for word in word_list:
    if word in text:
        print(f"The word '{word}' is present in the text.")
    else:
        print(f"The word '{word}' is not present in the text.")

2.要判断一个列表中的元素是否在另一个列表中,可以使用Python中的循环和条件判断语句来实现。以下是一个示例代码:

python 复制代码
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]

for element in list1:
    if element in list2:
        print(f"The element '{element}' is present in both lists.")
    else:
        print(f"The element '{element}' is not present in both lists.")

3.要判断列表中的元组的每个元素是否在一段文本中,可以使用Python中的循环和条件判断语句来实现。以下是一个示例代码:

python 复制代码
text = "Hello, how are you today?"
tuple_list = [("Hello", "how"), ("are", "you"), ("today", "Python")]

for t in tuple_list:
    if all(word in text for word in t):
        print(f"text可以找到这个{t} ")
    else:
        print(f"text不能,找到这个{t} ")
相关推荐
财经资讯数据_灵砚智能6 分钟前
基于全球经济类多源新闻的NLP情感分析与数据可视化(日间)2026年6月7日
人工智能·python·ai·信息可视化·自然语言处理·ai编程·灵砚智能
smith成长之旅13 分钟前
08 | Mem0 框架分析: BM25 的 Sigmoid 归一化
数据库·python·算法
DogDaoDao13 分钟前
【第 04 篇】列表与元组 —— 序列类型核心详解
人工智能·python·深度学习·神经网络·机器学习·conda·numpy
idingzhi14 分钟前
A股量化策略日报(2026年06月07日)
python
xingpanvip22 分钟前
使用 Webwright 在 CSDN 自动发文:Python 浏览器自动化实践
开发语言·python·自动化
armwind24 分钟前
openISP学习7-CCM — Color Correction Matrix(色彩校正矩阵)
python·学习·矩阵
艺杯羹25 分钟前
零成本!3步设置Windows动态壁纸,免费无广告
python
huangdong_35 分钟前
京东整店商品图片视频批量下载技术:从商品列表到自动分类
开发语言·python·音视频
糖果店的幽灵42 分钟前
Spring AI 从入门到精通-ChatClient你与 AI 对话的终极武器
人工智能·python·spring
海鸥-w1 小时前
用python (fastapi)做项目第二天实现新闻列表和新闻详情接口
开发语言·python·fastapi