Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #8114
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Subject | Re: Don't understand SequenceMatcher from difflib |
| Date | 2011-06-21 15:02 -0400 |
| References | <20110621134305.GA8929@trout.vub.ac.be> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.240.1308682992.1164.python-list@python.org> (permalink) |
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
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Don't understand SequenceMatcher from difflib Terry Reedy <tjreedy@udel.edu> - 2011-06-21 15:02 -0400
csiph-web