我必須編寫一個函數,它將一個字符串作爲參數,並將此字符串比較爲兩個其他字符串,並返回最相似的字符串和差異數。比較兩個字符串並返回最相似的一個
def func("LUMB"):
lst=["JIBM", "NUNE", "NUMB"]
should return:
("NUMB",1)
我曾嘗試:
def f(word):
lst=["JIBM", "NUNE", "NUMB"]
for i in lst:
d=k(word, lst)
return differences
for n in d:
print min(sum(n))
其中:
def k(word1, word2):
L=[]
for w in range(len(word1)):
if word1[w] != word2[w]:
L.append(1)
else:
L.append(0)
return L
,使我得到如列表,[1,0,0,0]如果字1 =「NUMB 「和word2 =」LUMB「
你見過[Text difference algorithm](http://stackoverflow.com/questions/145607/text-difference-algorithm)和[用於模糊字符串比較的好Python模塊](http://stackoverflow.com/questions)/682367 /好-python-modules-for-fuzzy-string-comparison) – Chris 2011-12-15 11:21:26
很多答案都可以在這個鏈接上獲得http://stackoverflow.com/questions/682367/good-python-modules-for-fuzzy -string-comparison – 2011-12-15 11:32:09