Skip to main content
 首页 » 编程设计

python之方法 set_seq1 和 set_seq2 的工作,difflib python

2025年05月04日7lovecherry

我已经检查了 difflib 的文档,我对如何 difflib.SequenceMatcher.ratio() 感到困惑确实有效。考虑一下:

s = difflib.SequenceMatcher(None, "hey here" , "hey there").ratio() 
print s  

给出 s = 0.9411764705882353
我想知道它是如何计算出来的。通过实际查看一个字符串与其他字符串的偏差来比较 2 个字符串。对于 2 个字符串 a 和 b 文档说:

差异被计算为“我们需要对‘a’做什么才能将其更改为‘b’?”

还有类似的东西:

对于 b 中的 x,b2j[x] 是 x 出现的索引列表(进入 b);垃圾元素不出现

请解释上面的 s 示例。

请您参考如下方法:

来自文档:

Where T is the total number of elements in both sequences, and M is the number of matches, this is 2.0*M / T.

在这种情况下,T 为 17,因为第一个字符串有 8 个字符,第二个字符串有 9 个。M 为 8,因为第一个字符串中的八个字符与第二个字符串中的字符匹配。 2 * 8/17 等于 0.9411764705882353。