python - 在单词列表中找到与给定单词距离最小的单词
问题描述
假设给定一个单词列表['windows','hello','python','world','software','desk']
和一个输入单词'widow'
,如何(快速)从单词列表中找到与输入单词具有最小编辑距离的单词'widow'
(本例中的答案是'windows'
)?是否有可用的库/函数来实现它?谢谢!
解决方案
有python-Levenshtein库。该distance()
功能是您正在寻找的。
关于清单,我会这样做:
input = "widow"
words = ['windows','hello','python','world','software','desk']
distances = [distance(input, word) for word in words]
closest = words[distances.index(min(distances)]
您将不得不处理两个单词的输入距离相同的情况。
推荐阅读
- docker - 无法使用 dockerfile 在容器中移动文件
- c - 仅使用寄存器的 2 个 Arduino Uno 之间的串行通信
- javascript - 正则表达式将驼峰大小写转换为普通句子,同时将一些转换为大写
- tcp - Wireshark如何通过头域判断一个协议是SMTP还是TCP?
- r - 在 purrr() 中使用 optim() 时出错:vmmin 不是有限的
- node.js - 第一次使用 node.js X 未定义
- python - 在python中的字符串匹配后无法删除行
- javascript - 返回映射行 NULL
- systemjs - 如何在我的 angular9 项目中使用 SystemJS
- python - 设置一定百分比的数据帧等于 NaN