假设我有一个长度为 29 的基本字符串,以及一个长度为 28 和 30 的任意字符串列表。我将如何确定这些字符串的数量,这些字符串可能是对基础字符串?
为了记录,我是用 Python 做的。
最佳答案
让我们看看...我会修改 Levenshtein distance algorithm (Python 代码 here )使其仅在添加或删除一个字符的情况下起作用。
from functools import partial
from my_distances import **add_delete_distance**
def is_accepted(base_string, alternative_string):
'''It uses the custom distance algorithm to evaluate (boolean output) if a
particular alternative string is ok with respect to the base string.'''
assert type(alternative_string) == str
len_difference = abs(len(base_string)-len(alternative_string))
if len_difference == 1 :
distance = add_delete_distance(base_string, alternative_string)
if distance == 1:
return True
return False
base_string = 'michele'
alternative_strings = ['michel', 'michelle', 'james', 'michela']
print filter(partial(is_accepted, base_string), alternative_string)
你怎么看?
关于python - 确定字符串 B 是否可能是对字符串 A 进行删除/添加的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24294510/