Python Reference ยท Standard Library: Math, Data and Text
The difflib module
Reference for Python's difflib module: compare strings and sequences, get similarity ratios, close matches for fuzzy lookup and unified diffs of text.
The difflib module compares sequences, most often lines of text or strings. It can tell you how similar two strings are, find the closest match to a word in a list (handy for "did you mean...?" messages), and produce diffs in the same style as the diff command.
Its core class is SequenceMatcher, which finds the longest matching blocks between two sequences. The result is a heuristic that reads well to people rather than a mathematically minimal edit script, and similarity scores are a rough guide, not a strict metric.
import difflib print(difflib.get_close_matches("pyton", ["python", "java", "ruby"])) # ['python']
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Main members
| Name | Description |
|---|---|
SequenceMatcher(isjunk=None, a='', b='', autojunk=True) |
Compares two sequences of hashable items. |
.ratio() |
Similarity from 0.0 to 1.0, calculated as 2 * M / T (M = matched items, T = total items in both). |
.quick_ratio(), .real_quick_ratio() |
Cheaper upper bounds of ratio(). |
.get_matching_blocks() |
List of (a_start, b_start, size) blocks that match, ending with a size-0 sentinel. |
.get_opcodes() |
Edit steps as (tag, i1, i2, j1, j2) with tags replace, delete, insert, equal. |
get_close_matches(word, possibilities, n=3, cutoff=0.6) |
Up to n best matches whose ratio is at least cutoff, best first. |
unified_diff(a, b, fromfile, tofile, n=3, lineterm="\n") |
Generator of unified diff lines (---, +++, @@, +, -). |
context_diff(...) |
The older context format. |
ndiff(a, b) / Differ().compare(a, b) |
Line-by-line differences with - , + , and ? hint lines. |
HtmlDiff |
Produces an HTML table of side-by-side differences. |
restore(diff, which) |
Rebuilds sequence 1 or 2 from an ndiff result. |
Usage notes
- Arguments are sequences of lines for diffs, not whole strings. Use
text.splitlines()(orkeepends=True) first. Passing a string compares character by character. lineterm. Unified diff header lines end with a newline by default. When lines come fromsplitlines()(no newlines), passlineterm=""and print each line.ratio()is not symmetric in every case;SequenceMatcher(None, a, b).ratio()can differ from the swapped call. Put the "reference" string inbwhen you compare many candidates, becauseSequenceMatchercaches data aboutb.autojunkcan hurt. For sequences of 200 or more items, popular elements are treated as junk, which can reduce match quality on long repetitive data. Passautojunk=Falseto disable it.- Not an edit distance. There is no Levenshtein distance here;
ratio()is a different measure. For strict edit distances write a dynamic-programming function or use a library. - Quadratic cost in the worst case. It is fine for documents and word lists, but slow for very large inputs.
- Fuzzy matching is case sensitive. Lowercase both sides if you want case-insensitive suggestions.
Related
str.splitlines(),str.casefold(),re,textwrap- Lessons: Strings, Algorithms