Comparing lists in python by difflib modul
Question
I am trying out the difflib library. I have two lists: L_1 and L_2 containing strings. I want to know, if those sequences are similar (order is not important).
L_1 = ["Bob", "Mary", "Hans"]
L_2 = ["Bob", "Marie", "Háns"]
should be ok. But
L_1 = ["Nirdosch", "Mary", "Rolf"]
L_2 = ["Bob", "Marie", "Háns"]
should not be ok.
I came up with the idea of iterating over the first list L_1 and to match every element of L_1 by the method
difflib.get_close_matches()
against the second list L_2. If there was a match with a ratio bigger then let's say 0.7 remove it from L_2 and continue. But I doubt it is a good plan. Is there a better one?
Solution
I would do something like:
import difflib
L_1 = ["Bob", "Mary", "Hans"]
L_2 = ["Bob", "Marie", "Hans"]
def similiarity(L_1, L_2):
L_1 = set(intern(w) for w in L_1)
L_2 = set(intern(w) for w in L_2)
to_match = L_1.difference( L_2)
against = L_2.difference(L_1)
for w in to_match:
res = difflib.get_close_matches(w, against)
if len(res):
against.remove( res[0] )
return (len(L_2)-len(against)) / (len(L_1))
print similiarity(L_1,L_2)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow