检查另一个字符串中是否存在多个字符串 [英] Check if multiple strings exist in another string
本文介绍了检查另一个字符串中是否存在多个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何检查数组中的任何字符串是否存在于另一个字符串中?
How can I check if any of the strings in an array exists in another string?
喜欢:
a = ['a', 'b', 'c']
str = "a123"
if a in str:
print "some of the strings found in str"
else:
print "no strings found in str"
那个代码不起作用,只是为了展示我想要实现的目标.
That code doesn't work, it's just to show what I want to achieve.
推荐答案
您可以使用 any
:
a_string = "A string is more than its parts!"
matches = ["more", "wholesome", "milk"]
if any(x in a_string for x in matches):
类似于检查是否找到了列表中的所有字符串,使用all
而不是 any
.
Similarly to check if all the strings from the list are found, use all
instead of any
.
这篇关于检查另一个字符串中是否存在多个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文