all Python 3difflibterm
difflib.SequenceMatcher
sequence matching
A class that compares two sequences (strings, lists of lines): the ratio() similarity score and a list of edits.
Syntax
SequenceMatcher(isjunk=None, a='', b='', autojunk=True).ratio()
ratio() is not symmetric: for 'tide' and 'diet' it is 0.25, and 0.5 the other way round. When the second sequence has 200 items or more, autojunk kicks in: items making up over 1% of it are treated as junk, and nearly identical long texts score 0.0; pass autojunk=False. ratio() is costly; quick_ratio() and real_quick_ratio() give a fast upper bound for filtering.
Examples
from difflib import SequenceMatcher
print(SequenceMatcher(None, 'abcd', 'bcde').ratio()) # → 0.75
print(SequenceMatcher(None, 'tide', 'diet').ratio()) # → 0.25
print(SequenceMatcher(None, 'diet', 'tide').ratio()) # → 0.5
from difflib import SequenceMatcher
a = 'x' + 'ab' * 150
b = 'y' + 'ab' * 150
print(SequenceMatcher(None, a, b).ratio()) # → 0.0 — сработал autojunk
print(round(SequenceMatcher(None, a, b, autojunk=False).ratio(), 3)) # → 0.997
See also
Open in the full glossarydocs.python.orgReport an inaccuracy