我想检查“a”中的某个词是否在“文本”中
text = "testing if this works"
a = ['asd' , 'test']
print text.find(a)
我该怎么做?
谢谢
请您参考如下方法:
如果你想检查 a 中的任何单词是否在文本中,那么使用 any :
any(word in text for word in a)
如果您想知道 a 中出现在文本中的单词数,您可以简单地 add他们:
print('Number of words in a that match text: %s' %
sum(word in text for word in a))
如果您只想匹配完整的单词(即您不想匹配 test 单词 testing),请将文本拆分为单词,如:
words = set(text.split())
any(word in words for word in a)
