Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 检查字符串是否包含任何列表元素_Python_List - Fatal编程技术网

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

Python 检查字符串是否包含任何列表元素,python,list,Python,List,所以我在做一个列表的for循环。每一个字符串,我都要.find,但不是.find一个字符串中的一项,我要检查该字符串在我的列表中的任何内容 比如说 checkfor = ['this','that','or the other'] 那就做吧 string.find(checkfor)或其他东西,因此我要执行以下操作: if email.find(anything in my checkforlist) == -1: do action 我想检查我列表中的任何字符串 Python在中

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

比如说

checkfor = ['this','that','or the other'] 
那就做吧

string.find(checkfor)或其他东西,因此我要执行以下操作:

if email.find(anything in my checkforlist) == -1:
    do action
我想检查我列表中的任何字符串

Python在中为此提供了

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

您可以尝试使用列表理解来实现这一点

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

子句中使用

If checkfor not in email:
    do_some_thing()

如果您只想知道字符串中是否至少存在列表中的一个值,那么一种简单的方法是:

any(email.find(check) > -1 for check in checkfor)
如果要检查字符串中是否存在所有这些值,请执行以下操作

all(email.find(check) > -1 for check in checkfor)
或者,如果您想要字符串中匹配的精确值,可以执行以下操作:

matches = [match for match in checkfor if email.find(match) > -1]
我更愿意使用:

check in email
结束

但我想这可能取决于您的用例(上面的例子可能会更好地使用
in
操作符)


根据您的情况,您可能更喜欢使用,但我在这里不作详细说明。

您是在尝试查找该列表中所有内容的位置,还是在尝试检查该列表中的任何内容是否包含在您的字符串中?
email.find(check) > -1