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