确定字符串B是否可能是删除/添加到字符串a的结果

2024-06-01 23:22:47 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有一个长度为29的基字符串,以及一个长度为28和30的任意字符串列表。我如何确定这些字符串的数量,这些字符串可能是在基字符串上执行的一个字符的删除/添加的结果?你知道吗

我是用Python写的,这是为了记录在案。你知道吗


Tags: 字符串列表数量字符记录在案
1条回答
网友
1楼 · 发布于 2024-06-01 23:22:47

让我们看看。。。我将修改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)

你觉得怎么样?你知道吗

相关问题 更多 >