我有一个单词列表
list = ['car', 'animal', 'house', 'animation']
我想将每个列表项与字符串 str1
进行比较,输出应该是最相似的词。示例:如果 str1
是 anlmal
,则 animal
是最相似的词。我怎么能在 python 中做到这一点?通常我列表中的词彼此之间可以很好地区分。
最佳答案
使用difflib :
difflib.get_close_matches(word, ['car', 'animal', 'house', 'animation'])
正如您从阅读 the source 中看到的那样,“接近”匹配项从最佳到最差排序。
>>> import difflib
>>> difflib.get_close_matches('anlmal', ['car', 'animal', 'house', 'animation'])
['animal']
关于python - 如何在python列表中找到最相似的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26283715/