我想有以下幾點:比較兩個字符串的相似性在Python
Input: ("a#c","abc")
Output: True
Input:("a#c","abd")
Desired Output: False
Real Output: True
所以該函數返回true,如果兩個字符串的長度相同,如果他們通過#字符,它表示只有不同爲隨機字符。 如果不是,我希望它返回False。
該功能應該改變什麼?
def checkSolution(problem, solution):
if len(problem) != len(solution):
return False
if len(problem) == len(solution):
for i, c in zip(problem, solution):
if i != c:
return False
if i == c or "#" == c:
return True
print (checkSolution("a#c","abc"))
print (checkSolution("a#c","abd"))
出了什麼問題?首先是縮進;) –
也許,很好的答案已經在這裏:http://stackoverflow.com/questions/1471153/string-similarity-metrics-in-python –
注意審稿人:雖然羅馬提供的鏈接有很多更好的方法比較字符串的相似性,它實際上並沒有回答爲什麼他當前的解決方案不起作用的問題 – Foon