You forgot the first parameter to SequenceMatcher.

>>> import difflib
>>> 
>>> a='abcd'
>>> b='ab123'
>>> seq=difflib.SequenceMatcher(None, a,b)
>>> d=seq.ratio()*100
>>> print d
44.4444444444

http://docs.python.org/library/difflib.html

Answer from Lennart Regebro on Stack Overflow
🌐
Python
docs.python.org › 3 › library › difflib.html
difflib — Helpers for computing deltas
This is a class for comparing sequences of lines of text, and producing human-readable differences or deltas. Differ uses SequenceMatcher both to compare sequences of lines, and to compare sequences of characters within similar (near-matching) lines.
🌐
Medium
medium.com › @zhangkd5 › a-tutorial-for-difflib-a-powerful-python-standard-library-to-compare-textual-sequences-096d52b4c843
A Tutorial of Difflib — A Powerful Python Standard Library to Compare Textual Sequences | by Kaidong Zhang | Medium
January 27, 2024 - from difflib import SequenceMatcher a = """The cat is sleeping on the red sofa.""" b = """The cat is sleeping on a blue sofa...""" seq_match = SequenceMatcher(None, a, b) ratio = seq_match.ratio() print(ratio) # Check the similarity of the two strings # The output similarity will be a decimal between 0 and 1, in our example it may output: # 0.821917808219178
🌐
Beautiful Soup
tedboy.github.io › python_stdlib › generated › generated › difflib.SequenceMatcher.html
difflib.SequenceMatcher — Python Standard Library
SequenceMatcher is a flexible class for comparing pairs of sequences of any type, so long as the sequence elements are hashable.
🌐
Educative
educative.io › answers › what-is-sequencematcher-in-python
What is SequenceMatcher() in Python?
SequenceMatcher is a class that is available in the difflib Python package.
🌐
Typesense
typesense.org › posts › fuzzy string matching in python (with examples)
Fuzzy string matching in Python (with examples) | Typesense
Let’s explore how we can utilize various fuzzy string matching algorithms in Python to compute similarity between pairs of strings. SequenceMatcher is available as part of the Python standard library.
Find elsewhere
🌐
GitHub
github.com › python › cpython › blob › main › Lib › difflib.py
cpython/Lib/difflib.py at main · python/cpython
i and in j. New in Python 2.5, it's also guaranteed that 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'. IOW, adjacent triples never describe adjacent equal · blocks. · The last triple is a dummy, (len(a), len(b), 0), and is the only · triple with n==0. · >>> s = SequenceMatcher(None, "abxcd", "abcd") >>> list(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)] """ ·
Author   python
🌐
GeeksforGeeks
geeksforgeeks.org › python › sequencematcher-in-python-for-longest-common-substring
SequenceMatcher in Python for Longest Common Substring - GeeksforGeeks
March 24, 2023 - # Function to find Longest Common Sub-string from difflib import SequenceMatcher def longestSubstring(str1,str2): # initialize SequenceMatcher object with # input string seqMatch = SequenceMatcher(None,str1,str2) # find match of longest sub-string # output will be like Match(a=0, b=0, size=5) match = seqMatch.find_longest_match(0, len(str1), 0, len(str2)) # print longest substring if (match.size!=0): print (str1[(match.a: match.a + match.size)]) else: print ('No longest common sub-string found') # Driver program if __name__ == "__main__": str1 = 'GeeksforGeeks' str2 = 'GeeksQuiz' longestSubstring(str1,str2)
🌐
HexDocs
hexdocs.pm › difflib › Difflib.SequenceMatcher.html
Difflib.SequenceMatcher — Difflib v0.1.0
SequenceMatcher is a flexible module for comparing pairs of sequences of any type, so long as the sequence elements are hashable.
🌐
Python
bugs.python.org › issue31889
Issue 31889: difflib SequenceMatcher ratio() still have unpredictable behavior - Python tracker
This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/76070
🌐
Amanxai
amanxai.com › home › all articles › sequencematcher in python
SequenceMatcher in Python | Aman Kharwal
March 3, 2022 - text1 = "My Name is Aman Kharwal" text2 = "I am the founder of thecleverprogrammer.com" sequenceScore = SequenceMatcher(None, text1, text2).ratio() print(f"Both are {sequenceScore * 100} % similar") ... So, according to the score above, it shows that both the text inputs have less similar sequences. This is how you can use this class in Python available in the difflib module.
🌐
GeeksforGeeks
geeksforgeeks.org › python › compare-sequences-in-python-using-dfflib-module
Compare sequences in Python using dfflib module - GeeksforGeeks
February 24, 2021 - Python3 · # import required module import difflib # assign parameters par1 = 'gfg' par2 = 'GFG' # compare print(difflib.SequenceMatcher(None, par1, par2).ratio()) Output: 0.0 · The get_matching_blocks() method of this class returns a list of triples describing matching subsequences.
🌐
Medium
medium.com › @user1337 › exploring-string-matching-and-diffing-algorithms-and-libraries-in-python-96461fbd28fc
Exploring String Matching and Diffing Algorithms and Libraries in Python | by User | Medium
January 25, 2023 - difflib is a Python library that provides classes for comparing sequences and producing human-readable differences between them. The SequenceMatcher class in difflib can be used to compare two strings and find the differences between them.
🌐
Python Forum
python-forum.io › thread-32469.html
matching with SequenceMatcher ratio two dataframe
Hello, I use the SequenceMatcher ratio to match two dataframe with the best ratio. I want to check first if the score A and AA is good then check if the score between B is BB is good then if the score between C and CC is good, then I add the line ...
🌐
PyPI
pypi.org › project › cdifflib
cdifflib · PyPI
Python difflib sequence matcher reimplemented in C. Actually only contains reimplemented parts. Creates a CSequenceMatcher type which inherets most functions from difflib.SequenceMatcher.
      » pip install cdifflib
    
Published   Jan 13, 2025
Version   1.2.9
🌐
GitHub
github.com › python › cpython › issues › 106865
A warning should be added to difflib documentation about SequenceMatcher performance · Issue #106865 · python/cpython
July 18, 2023 - Documentation Some functions of difflib.SequenceMatcher perform very poorly in real-world scenarios (for example get_opcodes() takes several minutes to return a list of tuples for two identical fil...
Author   maathieu
🌐
Reddit
reddit.com › r/learnpython › functions similar to difflib.sequencematcher.ratio()?
r/learnpython on Reddit: Functions similar to difflib.SequenceMatcher.ratio()?
March 16, 2018 -

I am currently using sequenceMatcher.ratio() in a program I am working on, and while the function itself is exactly what I need the runtime is an issue. On 2 files im testing on, 500x2000 lines it takes about 1 minute. On the actual target documents, 20000x20000, it will take around 4000 minutes or roughly 3 days as best as I can figure.

I can't use quick_ratio() or real_quick_ratio() because accuracy of comparisons matter and both quick_ratio() and real_quick_ratio() per the documentation are "always at least as large as ratio()", or in other words will say that words are more similar than the normal ratio function.

If anyone knows any similar functions or other ways of approaching this issue (comparing how similar two words are relatively quickly) I could really use the help. The only alternative I or my boss have at the moment is multiprocessing or pushing it into a distributed environment and just brute forcing the slow version I have at the moment.