Levenshtein distance between list of number

筅森魡賤 提交于 2020-01-16 18:20:13

问题


Have this code , i want to have levenshtein distance between two list of numbers.

import textdistance
S1=[1,2,3,7,9,15,19,20]
S2=[1,2,3,7,8,14,20]
#convert lists to string 
Str1=‘’.join(str(e) for e in S1)
Str2=‘’.join(str(e) for e in S2)
textdistance.levenshtein.similarity(Str1,Str2)
textdistance.levenshtein.distance(Str1,Str2)

The above code gives similarity of : 7 Which is wrong , the correct is 5 . And shows distance value of 4 , which wrong also, the correct distance is 3.

How to manipulate the code so that numbers like 14 and 15 can be seen as one element to the function above ? Any idea ..


回答1:


Do not convert to the string, keep the code just:

S1=[1,2,3,7,9,15,19,20]
S2=[1,2,3,7,8,14,20]

Textdistnace.levenshtein.similarity(S1,S2)
Textdistance.levenshtein.distance(S1,S2)



回答2:


Try to use jellyfish library as such:

import jellyfish
S1=[1,2,3,7,9,15,19,20]
S2=[1,2,3,7,8,14,20]
#convert lists to string 
Str1=‘’.join(str(e) for e in S1)
Str2=‘’.join(str(e) for e in S2)

jellyfish.levenshtein_distance(Str1, Str2)

You may find more info about it here: https://pypi.org/project/jellyfish/



来源:https://stackoverflow.com/questions/56597964/levenshtein-distance-between-list-of-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!