且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

计算两个列表相似

更新时间:2022-04-15 07:52:52

您可以使用 difflib 模块

比()
  回报范围内的浮动序列相似度的度量[0,1]。

ratio()
Return a measure of the sequences’ similarity as a float in the range [0, 1].

其中给出:

 >>> s1=[1,8,3,9,4,9,3,8,1,2,3]
 >>> s2=[1,8,1,3,9,4,9,3,8,1,2,3]
 >>> sm=difflib.SequenceMatcher(None,s1,s2)
 >>> sm.ratio()
 0.9565217391304348