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} ")
相关推荐
巨人张17 分钟前
信息素养Python编程题
开发语言·python
站大爷IP23 分钟前
Python爬虫动态IP代理报错全解析:从问题定位到实战优化
python
hie9889429 分钟前
CentOS环境搭建-快速升级G++版本
linux·python·centos
盛寒32 分钟前
向量空间 线性代数
python·线性代数·机器学习
5171 小时前
Django中序列化与反序列化
后端·python·django
lxiaoj1111 小时前
Python文件操作笔记
笔记·python
Tomorrow'sThinker2 小时前
✍️ Python 批量设置 Word 文档多级字体样式(标题/正文/名称/小节)
python·自动化·word·excel
秋难降2 小时前
Python 知识点详解(三)
python·编程语言
chao_7893 小时前
二分查找篇——寻找旋转排序数组中的最小值【LeetCode】
python·线性代数·算法·leetcode·矩阵
金玉满堂@bj3 小时前
PyCharm 中 Python 解释器的添加选项及作用
ide·python·pycharm