python - 如何在python列表中找到最相似的单词

标签 python

我有一个单词列表

list = ['car', 'animal', 'house', 'animation']

我想将每个列表项与字符串 str1 进行比较,输出应该是最相似的词。示例:如果 str1anlmal,则 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/

相关文章:

python - Python click 模块没有输出?

python - 迭代列表的奇怪速度差异

c++ - 交换两个序列的元素,使得元素和的差异最小。

python - 尝试检查 MySQL 数据库中是否已有值

python - 如何在 GitHub 上托管的 Python 脚本中保密开发人员 key

python - Elasticsearch-py 日期格式错误

python - Alembic Django Flask 迁移数据库

python - Pandas 内存错误

python - 如何在 Django 1.8 中使用 jinja2 作为模板引擎

python - 使用 SQLAlchemy 和 pymysql,如何设置连接以使用 utf8mb4?