所以我正在循环一个列表。 我想要对每个字符串使用.find,但是我不想为一个字符串查找一个项目,而是要检查该字符串是否包含列表中的任何内容。
例如:
checkfor = ['this','that','or the other']
然后执行 string.find(checkfor) 或者其他操作,所以我想这样做:
if email.find(anything in my checkforlist) == -1:
do action
所以我正在循环一个列表。 我想要对每个字符串使用.find,但是我不想为一个字符串查找一个项目,而是要检查该字符串是否包含列表中的任何内容。
例如:
checkfor = ['this','that','or the other']
然后执行 string.find(checkfor) 或者其他操作,所以我想这样做:
if email.find(anything in my checkforlist) == -1:
do action
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
结束
email.find(check) > -1
但我想这取决于您的使用情况(以上示例可能更适合使用in
运算符)。
根据您的情况,您可能更喜欢使用正则表达式,但我不会在这里详细介绍。
我希望能够检查那个字符串是否包含我的列表中的任何内容。
Python 中有一个 in
关键字可以实现该功能。
for s in checkfor:
if s in email:
# do action
occurrences = [i for i, x in enumerate(email) if x =='this']
in
子句:If checkfor not in email:
do_some_thing()