[英]compare two strings and assign score in python
I need to know whether we can compare 2 string of different length and check the substring is occured in given sring or not 我需要知道我们是否可以比较2个不同长度的字符串并检查子字符串是否出现在给定的字符串中
For example: 例如:
str1='abcdef '
str2='acbcdef'
here length are same but letter is different how can i assign score for this 这里的长度是相同的,但字母是不同的,我如何为此分配分数
Use difflib
: 使用
difflib
:
from difflib import SequenceMatcher
def similar(a, b):
return SequenceMatcher(None, a, b).ratio()
str1='abcdef '
str2='acbcdef'
print(similar(str1,str2))
Output: 输出:
0.8571428571428571
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.