Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #8114 > unrolled thread

Re: Don't understand SequenceMatcher from difflib

Started byTerry Reedy <tjreedy@udel.edu>
First post2011-06-21 15:02 -0400
Last post2011-06-21 15:02 -0400
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Don't understand SequenceMatcher from difflib Terry Reedy <tjreedy@udel.edu> - 2011-06-21 15:02 -0400

#8114 — Re: Don't understand SequenceMatcher from difflib

FromTerry Reedy <tjreedy@udel.edu>
Date2011-06-21 15:02 -0400
SubjectRe: Don't understand SequenceMatcher from difflib
Message-ID<mailman.240.1308682992.1164.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web