如何在Python中获得字符串与另一个字符串类似的概率?
我想获得像0.9(意味着90%)等十进制值.最好使用标准的Python和库.
例如
similar("Apple","Appel") #would have a high prob.
similar("Apple","Mango") #would have a lower prob.
解决方法:
有一个内置的.
from difflib import SequenceMatcher
def similar(a, b):
return SequenceMatcher(None, a, b).ratio()
使用它:
>>> similar("Apple","Appel")
0.8
>>> similar("Apple","Mango")
0.0