如何使用SequenceMatcher查找两个字符串之间的相似性? [英] How to use SequenceMatcher to find similarity between two strings?

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

问题描述

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

我使用了上面的代码,但获得的输出为0.0.如何获得有效答案?

I used the above code but obtained output is 0.0. How can I get a valid answer?

推荐答案

您忘记了SequenceMatcher的第一个参数.

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

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

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