Python匹配一组中至少3个单词
我有一个带有短语的 str ,例如:
phrase = "My cat has two eyes and like to catch rats"
我有一组words我想在短语中匹配至少 3 个这些词。
words = set(["eyes", "like", "cat"])
目前我有以下代码
found = bool(set(phrase.lower().split()) & words)
但是如果有任何单词在短语中,它就会匹配,我想要至少 3 个单词匹配。
我能做些什么来实现这一目标?我不想使用regex.
回答
您可以检查交叉点的长度是否至少为 3。
found = len(set(phrase.lower().split()).intersection(words)) >= 3