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} ")
相关推荐
失败尽常态5232 小时前
用Python实现Excel数据同步到飞书文档
python·excel·飞书
2501_904447742 小时前
OPPO发布新型折叠屏手机 起售价8999
python·智能手机·django·virtualenv·pygame
青龙小码农2 小时前
yum报错:bash: /usr/bin/yum: /usr/bin/python: 坏的解释器:没有那个文件或目录
开发语言·python·bash·liunx
大数据追光猿3 小时前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
Leuanghing3 小时前
【Leetcode】11. 盛最多水的容器
python·算法·leetcode
xinxiyinhe4 小时前
如何设置Cursor中.cursorrules文件
人工智能·python
诸神缄默不语5 小时前
如何用Python 3自动打开exe程序
python·os·subprocess·python 3
橘子师兄5 小时前
分页功能组件开发
数据库·python·django
Logintern095 小时前
使用VS Code进行Python编程的一些快捷方式
开发语言·python
Multiple-ji6 小时前
想学python进来看看把
开发语言·python