如何检查数组中的任何字符串是否存在于另一个字符串中?

例如:

a = ['a', 'b', 'c']
s = "a123"
if a in s:
    print("some of the strings found in s")
else:
    print("no strings found in s")

我如何替换如果a在s:行得到适当的结果?


当前回答

flog = open('test.txt', 'r')
flogLines = flog.readlines()
strlist = ['SUCCESS', 'Done','SUCCESSFUL']
res = False
for line in flogLines:
     for fstr in strlist:
         if line.find(fstr) != -1:
            print('found') 
            res = True


if res:
    print('res true')
else: 
    print('res false')

其他回答

python文档中推荐的regex模块支持这一点

words = {'he', 'or', 'low'}
p = regex.compile(r"\L<name>", name=words)
m = p.findall('helloworld')
print(m)

输出:

['he', 'low', 'or']

实现的一些细节:link

这取决于上下文 假设你想检查单个文字(任何单个单词a,e,w,..等)就足够了

original_word ="hackerearcth"
for 'h' in original_word:
      print("YES")

如果你想检查original_word中的任何一个字符: 利用

if any(your_required in yourinput for your_required in original_word ):

如果您想要original_word中的所有输入,请使用所有输入 简单的

original_word = ['h', 'a', 'c', 'k', 'e', 'r', 'e', 'a', 'r', 't', 'h']
yourinput = str(input()).lower()
if all(requested_word in yourinput for requested_word in original_word):
    print("yes")

只是关于如何在String中获得所有列表元素的更多信息

a = ['a', 'b', 'c']
str = "a123" 
list(filter(lambda x:  x in str, a))

你可以使用任何:

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。

为了提高速度,我会使用这样的函数:

def check_string(string, substring_list):
    for substring in substring_list:
        if substring in string:
            return True
    return False