查找两个字符串之间的相似性度量
问题描述:
如何获得一个字符串与Python中另一个字符串相似的概率?
How do I get the probability of a string being similar to another string in Python?
我想获得一个像0.9(表示90%)之类的十进制值.最好使用标准Python和库.
I want to get a decimal value like 0.9 (meaning 90%) etc. Preferably with standard Python and library.
例如
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