我正在尝试逐个匹配两个字符串,直到第一个不匹配的字符,并确定百分比的完全匹配度。我的代码如下所示:
def match(a, b):
a, b = list(a), list(b)
count = 0
for i in range(len(a)):
if (a[i]!= b[i]): break
else: count = count + 1
return count/len(a)
a = '354575368987943'
b = '354535368987000'
c = '354575368987000'
print(match(a,b)) # return 0.267
print(match(a,c)) # return 0.8
Python中是否已经内置了可更快地执行此操作的方法? 为简单起见,假设这两个字符串具有相同的长度。
difflib
的SequenceMatcher.get_matching_blocks
:http://ideone.com/wlUVd9 - Ashwini Chaudharylist()
。 - TemporalWolf