在Python中有没有一种标准的方法来模糊地匹配一个字符串和任意可接受值的列表?

2024-10-01 15:36:09 发布

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

我希望有这样一个函数:

def findSimilar(string, options):
    ....
    return aString

其中aString与传递的字符串相似,但存在于options中。我使用这个函数来规范化我正在工作的玩具应用程序中的用户输入。我读过关于使用levenshtein distance的文章,但我决定在这里提问,因为我希望python标准库中有一个简单的解决方案。在


Tags: 函数字符串用户应用程序stringreturndef文章
3条回答

计算Levenshtein距离:

http://en.wikipedia.org/wiki/Levenshtein_distance

已经有了python实现,尽管我不知道它们的质量。。。在

使用^{}.get_close_matches。在

get_close_matches(word, possibilities[, n][, cutoff])

Return a list of the best “good enough” matches. word is a sequence for which close matches are desired (typically a string), and possibilities is a list of sequences against which to match word (typically a list of strings).

我想你应该看看这个帖子。你只需要一个模糊字符串比较器。在

https://stackoverflow.com/questions/682367/good-python-modules-for-fuzzy-string-comparison

相关问题 更多 >

    热门问题