在python中评估字符串中子串的存在的最佳性能方法?

die*_*lar 1 python string

我知道我能做到:

word = 'shrimp'
sentence = 'They eat shrimp in Mexico, and even more these days'
word in sentence
Run Code Online (Sandbox Code Playgroud)

所以它会评估True.

但如果我有:

words = ['a','la','los','shrimp']
Run Code Online (Sandbox Code Playgroud)

如何判断句子是否包含任何words元素?

我关心性能,因为列表可能很大,我不想使用循环扩展代码行

Ram*_*pte 7

可以读成英文.但可能不是最高效的.第一个基准,然后仅在需要时使用优化的解决方案.

any(word in sentence for word in words)
Run Code Online (Sandbox Code Playgroud)

any如果任何元素为True,则返回True,否则返回False.(word in sentence for word in words)是一个可迭代的,它产生每个单词是否在句子中.