64
>>> s = SequenceMatcher(None, " abcd", "abcd abcd")
>>> s.find_longest_match(0, 5, 0, 9)
Match(a=0, b=4, size=5)
If isjunk was provided, first the longest matching block is determined as above, but
with the additional restriction that no junk element appears in the block. Then that
block is extended as far as possible by matching (only) junk elements on both
sides. So the resulting block never matches on junk except as identical junk
happens to be adjacent to an interesting match.
Here’s the same example as before, but considering blanks to be junk. That
prevents
' abcd'
from matching the
' abcd'
at the tail end of the second
sequence directly. Instead only the
'abcd'
can match, and matches the leftmost
'abcd'
in the second sequence:
>>> s = SequenceMatcher(lambda x: x==" ", " abcd", "abcd abcd")
>>> s.find_longest_match(0, 5, 0, 9)
Match(a=1, b=0, size=4)
If no blocks match, this returns
(alo, blo, 0)
.
Changed in version 2.6: This method returns a named tuple
Match(a, b, size)
.
get_matching_blocks
()
Return list of triples describing matching subsequences. Each triple is of the form
(i, j, n)
, and means that
a[i:i+n] == b[j:j+n]
. The triples are monotonically
increasing in i and j.
The last triple is a dummy, and has the value
(len(a), len(b), 0)
. It is the only
triple with
n == 0
. If
(i, j, n)
and
(i', j', n')
are adjacent triples in the list,
and the second is not the last triple in the list, then
i+n != i'
or
j+n != j'
; in
other words, adjacent triples always describe non-adjacent equal blocks.
Changed in version 2.5: The guarantee that adjacent triples always describe non-
adjacent blocks was implemented.
>>> s = SequenceMatcher(None, "abxcd", "abcd")
>>> s.get_matching_blocks()
[Match(a=0, b=0, size=2), Match(a=3, b=2, size=2), Match(a=5, b=4, size=0)]
get_opcodes
()
Return list of 5-tuples describing how to turn a into b. Each tuple is of the form
(tag, i1, i2, j1, j2)
. The first tuple has
i1 == j1 == 0
, and remaining tuples