使用递归查找两个字符串的编辑距离

时间:2014-02-16 06:19:39

标签: python string

我需要使用递归来查找两个字符串的编辑距离,即我给函数两个参数(每个参数都是不同的sring)。该功能会找到将s1更改为s2所需的最少量更改。这就是我到目前为止所做的:

def edit_distance(s1,s2):
    split1 = list(s1)
    split2 = list(s2)
    count = 0
    pos = 0

    if split1[pos] == split2[pos]:
        pos += 1
    else:
        pos +=1
        count += 1
        edit_distance(s1, s2)

return count #This should be the minimum amount required to match the two strings

1 个答案:

答案 0 :(得分:1)

我注释了您的代码以向您显示代码流。我希望您现在明白为什么会收到错误:

def edit_distance(s1,s2):
    split1 = list(s1)    # Split strings into characters
    split2 = list(s2)
    count = 0            # This variable is local, it is not shared through calls to the function!
    pos = 0              # Same

    if split1[pos] == split2[pos]:   # pos is always 0 here!
        pos += 1                     # pos is incremented anyway, in if but also in else !
    else:
        pos +=1                      # See above
        count += 1                   # count is incremented, now it is 1
        edit_distance(s1, s2)        # recursive call, but with the identical arguments as before! The next function call will do exactly the same as this one, resulting in infinite recursion!

return count    # Wrong indentation here

您的功能无法满足您的需求。如果你在谈论汉明距离,这对我来说还不是很清楚,这里有一个示例实现,假设两个字符串的长度相等:

# Notice that pos is passed between calls and initially set to zero
def hamming(s1, s2, pos=0):    
    # Are we after the last character already?
    if pos < len(s1):
        # Return one if the current position differs and add the result for the following positions (starting at pos+1) to that
        return (s1[pos] != s2[pos]) + hamming(s1, s2, pos+1)
    else:
        # If the end is already reached, the remaining distance is 0
        return 0