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} ")
相关推荐
独行soc25 分钟前
2025年渗透测试面试题总结-67(题目+回答)
网络·python·安全·web安全·网络安全·adb·渗透测试
eybk1 小时前
用python的socket写一个局域网传输文件的程序
服务器·网络·python
程序员的世界你不懂1 小时前
【Flask】实现一个前后端一体的项目-脚手架
后端·python·flask
花酒锄作田1 小时前
[MCP][01]简介与概念
python·llm·mcp
Python私教1 小时前
Django全栈班v1.04 Python基础语法 20250912 上午
后端·python·django
言之。1 小时前
Django REST框架:ModelViewSet全面解析
数据库·python·django
Pocker_Spades_A2 小时前
Python快速入门专业版(二十六):Python函数基础:定义、调用与返回值(Hello函数案例)
开发语言·python
周周记笔记2 小时前
学习笔记:Python的起源
开发语言·python
魂尾ac3 小时前
Django + Vue3 前后端分离技术实现自动化测试平台从零到有系列 <第一章> 之 注册登录实现
后端·python·django·vue
Source.Liu3 小时前
【Pywinauto库】10.7 pywinauto.controls.uia_controls控件
windows·python·自动化