Python如何计算外部字典匹配字符串的个数

How to calculate the number of matching string with external dictionary in Python

我使用其他来源创建了一个字典,我正在尝试计算句子中匹配词和不匹配词的数量。

dictionary = ["hello", "good", "happy"]
str1 = ["the dog is happy"]

例如上面的例子,我想找出str1中与字典匹配的单词的数量和不匹配的数量,结果是匹配的数量= 1和不匹配的数量匹配 = 3。这可能吗?

d = ["hello", "good", "happy"]
s = "the dog is happy"

注意:我转 ["the dog is happy"] --> "the dog is happy" 因为列表是不必要的

l = [i in d for i in s.split(' ')]
matching = l.count(True)  # can also use sum(l)
non_matching = l.count(False)