Percentage matching of text

Tim Peters tim.peters at
Fri Jul 30 19:01:28 CEST 2004

[Bruce Eckel]
> What I'd like to do is find an algorithm that produces the results of
> a text comparison as a percentage-match. Thus I would be able to
> assert that my test samples must match the control sample by at least
> (for example) 83% for the test to pass.

>>> from difflib import SequenceMatcher as sm
>>> sm(None, 'abc', 'xyz').ratio()
>>> sm(None, 'abcd', 'abcd').ratio()
>>> sm(None, 'abcd', 'uvwx').ratio()
>>> sm(None, 'abcd', 'axyd').ratio()

SequenceMatcher works on sequences of hashable elements.  Above, it's
working on sequence of characters (aka "strings" <wink>).  Other
possibilites include sequences of lines ("files") and lists of

More information about the Python-list mailing list