Don't understand SequenceMatcher from difflib
Terry Reedy
tjreedy at udel.edu
Tue Jun 21 15:02:57 EDT 2011
On 6/21/2011 9:43 AM, Antoon Pardon wrote:
> matcher = SequenceMatcher(ls1, ls2)
...
> What am I doing wrong?
Read the doc, in particular, the really stupid signature of the class:
"class difflib.SequenceMatcher(isjunk=None, a='', b='', autojunk=True)"
You are passing isjunk = ls1, a = ls2, and by default, b=''. So there
are no matches, len(a) = 36, len(b) = 0, and the dummy match is (36,0,0)
as you got.
There are also several example in the doc, all like
>>> s = SequenceMatcher(None, " abcd", "abcd abcd") # or
>>> s = SequenceMatcher(lambda x: x==" ", " abcd", "abcd abcd")
So you will get better results with
matcher = SequenceMatcher(None, ls1, ls2) # or
matcher = SequenceMatcher(a=ls1, b=ls2)
In the future, please try to simply examples before posting for help.
print(list(SequenceMatcher('a','abc').get_matching_blocks()))
shows the problem you posted in one easily read line of input and
output. I only waded through the distracting code and output you posted
to find that problem because I patched SequenceMatcher last fall.
--
Terry Jan Reedy
More information about the Python-list
mailing list