python - 确定字符串 B 是否可能是对字符串 A 进行删除/添加的结果

标签 python string algorithm insertion

假设我有一个长度为 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/

相关文章:

python - 加权页面排名图表示

Python:回文(字符串索引超出范围)

python - 如何在Python中循环显示多个散点图?

java - 如何将打印功能合并到计算周长和识别形状的程序中?

c++ - 背包算法,如何获得更好的性能?

java - InterviewStreet 不友好数字 java

python - 从多个 shell 子进程中非阻塞实时读取 (Python)

python - 如何将 kivy 中的下拉列表与主按钮中心对齐?

string - 如何在VHDL中将字符串转换为整数?

string - 将 http 响应压缩为字符串的精确副本