假设两个列表的长度相同,并且您有一些函数grade(a,b) 其中a,b 是字符串:
key = ['freeze','dog','difficult','answer']
ans = ['freaze','dot','dificult','anser']
pairs = zip(key, ans)
score = sum(grade(k,v) for (k,v) in pairs)
一个可能的评分函数是:
def grade(a,b):
return 1 if a == b else -1
惩罚每个错误字符并为正确拼写给予 1 分的评分功能(听起来很苛刻......)可能是:
def grade(a,b):
score = sum(a == b for (a,b) in zip(a,b)) - max(len(a), len(b))
return score if score else 1
如果您想要 Levenshtein 距离,您可能希望您的 grade 函数成为以下内容的包装器,该功能在 Wikibooks 上找到并且似乎相当有效:
def levenshtein(seq1, seq2):
oneago = None
thisrow = range(1, len(seq2) + 1) + [0]
for x in xrange(len(seq1)):
twoago, oneago, thisrow = oneago, thisrow, [0] * len(seq2) + [x + 1]
for y in xrange(len(seq2)):
delcost = oneago[y] + 1
addcost = thisrow[y - 1] + 1
subcost = oneago[y - 1] + (seq1[x] != seq2[y])
thisrow[y] = min(delcost, addcost, subcost)
return thisrow[len(seq2) - 1]
您还可以查看difflib 来做更复杂的事情。