我想检查“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)
关于text - 在 python 中查找许多元素中的第一个 - text.find(a , b , c),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8884543/