Calculating the similarity of two lists

前端 未结 6 1288
野趣味
野趣味 2020-12-24 03:55

I have two lists:

eg. a = [1,8,3,9,4,9,3,8,1,2,3] and b = [1,8,1,3,9,4,9,3,8,1,2,3]

Both contain ints. There is no meaning behind the ints (eg. 1 is not \'cl

6条回答
  •  有刺的猬
    2020-12-24 04:23

    You can use the difflib module

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

    Which gives :

     >>> 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
    

提交回复
热议问题