假设我有以下列表:
l = ['foo', 'bar', 'baz']
我想知道搜索大文本的最快方法是什么,如果文本中存在任何字符串,则返回True
?
答案 0 :(得分:4)
text = 'slfdk gaklsdjfl asdkfljasdkljf qkwlejlqwekj bazaklajsdfkj gsadf'
l = ['foo', 'bar', 'baz']
print any(e in text for e in l)
答案 1 :(得分:1)
使用in
关键字,您可以轻松完成此操作:
def wordInText(list, text):
for word in list:
if word in text: return True
return False
wordInText(['test', 'cat', 'exam'], 'this is a simple example') # returns True
wordInText(['test', 'cat', 'max'], 'this is a simple example') # returns False
答案 2 :(得分:0)
import re
s = 'fgfkgfgujndf foofsdjbnfbarfkdfmdsf'
l = ['foo', 'bar', 'baz']
found = re.findall('|'.join(l), s)
if found:
print found