检查字符串是否包含任何列表元素

2024-09-27 22:36:19 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我在做一个列表的for循环。每一个字符串,我都想。查找,但不是。为一个字符串找到一个项,我要检查该字符串中的任何内容。在

例如。在

checkfor = ['this','that','or the other'] 

那就去吧

在字符串。查找(检查)或者别的什么,所以我想这样做:

^{pr2}$

Tags: orthe字符串内容列表forthatthis
3条回答

你可以尝试使用列表理解来完成这项工作。在

occurrences = [i for i, x in enumerate(email) if x =='this']

I want to check that string for anything in my list

Python对此有in。在

for s in checkfor:
    if s in email:
        # do action 

使用in子句:

If checkfor not in email:
    do_some_thing()

相关问题 更多 >

    热门问题