How to use SequenceMatcher to find similarity between two strings?

后端 未结 2 678
悲&欢浪女
悲&欢浪女 2020-12-24 06:09
import difflib

a=\'abcd\'
b=\'ab123\'
seq=difflib.SequenceMatcher(a=a.lower(),b=b.lower())
seq=difflib.SequenceMatcher(a,b)
d=seq.ratio()*100
print d
2条回答
  •  Happy的楠姐
    2020-12-24 06:49

    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

提交回复
热议问题