查找两个字符串之间的相似性度量 [英] Find the similarity metric between two strings

查看:96
本文介绍了查找两个字符串之间的相似性度量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获得一个字符串与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

这篇关于查找两个字符串之间的相似性度量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆