首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在下面的代码中使用get_close_matches?

如何在下面的代码中使用get_close_matches?
EN

Stack Overflow用户
提问于 2020-04-14 12:49:42
回答 1查看 536关注 0票数 0

我正在试着建立一个交互式的字典。我使用get close matches返回拼写错误的结果。有人能指出我的elif出了什么问题吗?我是python的新手

代码语言:javascript
运行
复制
import mysql.connector
import difflib
from difflib import SequenceMatcher
from difflib import get_close_matches

con = mysql.connector.connect(
user = "ardit700_student",
password = "ardit700_student",
host = "108.167.140.122",
database = "ardit700_pm1database",
)

word = input("Enter the word: ")
cursor = con.cursor() #create object to navigate through the page
query = cursor.execute("select * from Dictionary where Expression = '%s'" %word) #use cursor to execute the query
results = cursor.fetchall() # store all the results in a variable

if results:
    for result in results:
        print(result)
elif get_close_matches(word, cursor.stored_results()):
    print(results)
else:
    print("word not found")
EN

回答 1

Stack Overflow用户

发布于 2020-06-23 03:41:35

要使其正常工作,您可以使用列表理解,并从db获取键的列表。

代码语言:javascript
运行
复制
query1 = cursor.execute("SELECT Expression from Dictionary")
keys = [b[0] for b in cursor.fetchall()]

然后,当您使用get_close_matches()时,您将执行以下操作:

代码语言:javascript
运行
复制
get_close_matches(word, keys)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61200878

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档