python - Python 字典键/值抓取
问题描述
我目前是 Python 新手。
我正在制作一个字典应用程序来学习。
但是,我无法在我的一个函数中获得正确的输出。
我希望用户输入一个单词并返回单词(键)和定义(值)。
我从中提取的 JSON 文件可以在这里找到:https ://github.com/prestonjohnson17/Dictionary
import json
data = json.load(open("data.json"))
type(data)
def finding_def():
user_word = data[input(str())]
if data.keys() == user_word:
print(user_word)
else:
print ("not a real word")
finding_def()
解决方案
您应该检查该键是否存在于字典中,然后获取该键的值(尽管正如我看到的 JSON 文件,值本身是一个数组;您应该处理打印该数组的所有条目)。
def finding_def():
user_word = input()
if user_word in data:
print("Entries:")
for entry in data[user_word]:
print(entry)
else:
print("not a real word")
finding_def()
推荐阅读
- sql - 如何在 SQL Server 中将数字转换为 nvarchar?
- python-3.x - 从 PDF 中提取格式错误的文本
- java - 错误:java.lang.SecurityException:禁止的包名:java.lang
- jestjs - 在 React-Testing-Library 中使用自定义 /pages/_app.js 测试 Next.js 应用程序
- java - 使用 mouseClicked() 获取另一个图像
- html - 如何修复 {:multiple => true} 导致 nil:NilClass 的未定义方法 stringify_keys?
- python - 将 Pyspark Dataframe 中的数据导出到 Dictionary 或 List 以进一步处理 Python
- html - 获取请求,授权
- c++ - 似乎是由特定情况下的整数溢出引起的错误
- normalizr - 如何检查函数参数是否是 Normalizr 模式类?