how to diff / align Python lists using arbitrary matching function?
Posted
by James Tauber
on Stack Overflow
See other posts from Stack Overflow
or by James Tauber
Published on 2010-04-27T04:49:02Z
Indexed on
2010/04/27
4:53 UTC
Read the original article
Hit count: 214
I'd like to align two lists in a similar way to what difflib.Differ
would do except I want to be able to define a match function for comparing items, not just use string equality, and preferably a match function that can return a number between 0.0 and 1.0, not just a boolean.
So, for example, say I had the two lists:
L1 = [('A', 1), ('B', 3), ('C', 7)]
L2 = ['A', 'b', 'C']
and I want to be able to write a match function like this:
def match(item1, item2):
if item1[0] == item2:
return 1.0
elif item1[0].lower() == item2.lower():
return 0.5
else:
return 0
and then do:
d = Differ(match_func=match)
d.compare(L1, L2)
and have it diff using the match function. Like difflib
, I'd rather the algorithm gave more intuitive Ratcliff-Obershelp type results rather than a purely minimal Levenshtein distance.
© Stack Overflow or respective owner