You have basically three solutions: 1) write your own implementation of diff
; 2) hack the difflib
module; 3) find a workaround.
Your own implementation
In case 1), you could look at this question
and read a few books like CLRS or the books of Robert Sedgewick.
Hack the difflib
module
In case 2), have a look at the source code: get_matching_blocks
calls find_longest_match
at line 479. In the core of find_longest_match
, you have the b2j
dictionary that maps elements of the list a
to their indexes in the list b
. If you overwrite this dictionary, you can achieve what you want. Here's the standard version:
>>> import difflib
>>> from difflib import SequenceMatcher
>>> list3 = ["orange","apple","lemons","grape"]
>>> list4 = ["pears", "oranges","apple", "lemon", "cherry", "grapes"]
>>> s = SequenceMatcher(None, list3, list4)
>>> s.get_matching_blocks()
[Match(a=1, b=2, size=1), Match(a=4, b=6, size=0)]
>>> [(b.a+i, b.b+i, list3[b.a+i], list4[b.b+i]) for b in s.get_matching_blocks() for i in range(b.size)]
[(1, 2, 'apple', 'apple')]
Here's the hacked version:
>>> s = SequenceMatcher(None, list3, list4)
>>> s.b2j
{'pears': [0], 'oranges': [1], 'apple': [2], 'lemon': [3], 'cherry': [4], 'grapes': [5]}
>>> s.b2j = {**s.b2j, 'orange':s.b2j['oranges'], 'lemons':s.b2j['lemon'], 'grape':s.b2j['grapes']}
>>> s.b2j
{'pears': [0], 'oranges': [1], 'apple': [2], 'lemon': [3], 'cherry': [4], 'grapes': [5], 'orange': [1], 'lemons': [3], 'grape': [5]}
>>> s.get_matching_blocks()
[Match(a=0, b=1, size=3), Match(a=3, b=5, size=1), Match(a=4, b=6, size=0)]
>>> [(b.a+i, b.b+i, list3[b.a+i], list4[b.b+i]) for b in s.get_matching_blocks() for i in range(b.size)]
[(0, 1, 'orange', 'oranges'), (1, 2, 'apple', 'apple'), (2, 3, 'lemons', 'lemon'), (3, 5, 'grape', 'grapes')]
This is not hard to automate, but I wouldn't recommend you that solution, since there is a very simple workaround.
A workaround
The idea is to group words by families:
families = [{"pears", "peras"}, {"orange", "oranges", "naranjas"}, {"apple", "manzana"}, {"lemons", "lemon", "limón"}, {"cherry", "cereza"}, {"grape", "grapes"}]
It's now easy to create a dictionary that takes maps every word of the family to one of those words (let's call it the main word):
>>> d = {w:main for main, *alternatives in map(list, families) for w in alternatives}
>>> d
{'pears': 'peras', 'orange': 'naranjas', 'oranges': 'naranjas', 'manzana': 'apple', 'lemon': 'lemons', 'limón': 'lemons', 'cherry': 'cereza', 'grape': 'grapes'}
Note that main, *alternatives in map(list, families)
unpacks the family into a main word (the first of the list) and a list of alternatives using the star operator:
>>> head, *tail = [1,2,3,4,5]
>>> head
1
>>> tail
[2, 3, 4, 5]
You can then convert the lists to use only main words:
>>> list3=["orange","apple","lemons","grape"]
>>> list4=["pears", "oranges","apple", "lemon", "cherry", "grapes"]
>>> list5=["peras", "naranjas", "manzana", "limón", "cereza", "uvas"]
>>> [d.get(w, w) for w in list3]
['naranjas', 'apple', 'limón', 'grapes']
>>> [d.get(w, w) for w in list4]
['peras', 'naranjas', 'apple', 'limón', 'cereza', 'grapes']
>>> [d.get(w, w) for w in list5]
['peras', 'naranjas', 'apple', 'limón', 'cereza', 'uvas']
The expression d.get(w, w)
will return d[w]
if w
is a key, else w
itself. Hence, the words that belong to a family are converted to the main word of that family and the other words are left untouched.
Those lists are easy to compare with the difflib
.
Important: the time complexity of the conversion of the lists is negligible compared to the diff algorithm, hence you should not see the difference.
Full code
As a bonus, the full code:
def match_seq(list1, list2):
"""A generator that yields matches of list1 vs list2"""
s = SequenceMatcher(None, list1, list2)
for block in s.get_matching_blocks():
for i in range(block.size):
yield block.a + i, block.b + i # you don't need to store the matches, just yields them
def create_convert(*families):
"""Return a converter function that converts a list
to the same list with only main words"""
d = {w:main for main, *alternatives in map(list, families) for w in alternatives}
return lambda L: [d.get(w, w) for w in L]
families = [{"pears", "peras"}, {"orange", "oranges", "naranjas"}, {"apple", "manzana"}, {"lemons", "lemon", "limón"}, {"cherry", "cereza"}, {"grape", "grapes", "uvas"}]
convert = create_convert(*families)
list3=["orange","apple","lemons","grape"]
list4=["pears", "oranges","apple", "lemon", "cherry", "grapes"]
list5=["peras", "naranjas", "manzana", "limón", "cereza", "uvas"]
print ("list3 vs list4")
for a,b in match_seq(convert(list3), convert(list4)):
print(a,b, list3[a],list4[b])
# list3 vs list4
# 0 1 orange oranges
# 1 2 apple apple
# 2 3 lemons lemon
# 3 5 grape grapes
print ("list3 vs list5")
for a,b in match_seq(convert(list3), convert(list5)):
print(a,b, list3[a],list5[b])
# list3 vs list5
# 0 1 orange naranjas
# 1 2 apple manzana
# 2 3 lemons limón
# 3 5 grape uvas