Python Glossary Русский

all Python 3difflibterm

difflib.Match

sequence matching

The named tuple Match(a, b, size): a matching block a[a:a+size] == b[b:b+size] of two sequences.

Syntax

Match(a, b, size)

It is returned by SequenceMatcher's find_longest_match() and get_matching_blocks(). The trap: the last item of get_matching_blocks() is always an empty sentinel Match(len(a), len(b), 0), and a loop counting matches must skip it. Among several equally long matches, find_longest_match() picks the earliest one in a.

Examples

from difflib import SequenceMatcher
sm = SequenceMatcher(None, 'abxcd', 'abcd')
print(sm.get_matching_blocks())  # → [Match(a=0, b=0, size=2), Match(a=3, b=2, size=2), Match(a=5, b=4, size=0)]
m = sm.find_longest_match(0, 5, 0, 4)
print(m.a, m.size, 'abxcd'[m.a:m.a + m.size])  # → 0 2 ab

See also

Open in the full glossarydocs.python.orgReport an inaccuracy