python - 如何使用 PyDictionary 获得一个单词的含义?
问题描述
我想要实现的是能够使用 PyDictionary 选择一个单词的随机含义,使用以下代码:
word = dic.meaning('book')
print(word)
到目前为止,这仅输出一长串含义,而不仅仅是一个。
{'Noun': ['a written work or composition that has been published (printed on pages bound together', 'physical objects consisting of a number of pages bound together', 'a compilation of the known facts regarding something or someone', 'a written version of a play or other dramatic composition; used in preparing for a performance', 'a record in which commercial accounts are recorded', 'a collection of playing cards satisfying the rules of a card game', 'a collection of rules or prescribed standards on the basis of which decisions are made', 'the sacred writings of Islam revealed by God to the prophet Muhammad during his life at Mecca and Medina', 'the sacred writings of the Christian religions', 'a major division of a long written composition', 'a number of sheets (ticket or stamps etc.'], 'Verb': ['engage for a performance', 'arrange for and reserve (something for someone else', 'record a charge in a police register', 'register in a hotel booker']}
我试图给我的第一个含义是:
word = dic.meaning('book')
print(word[1])
但这样做会导致此错误:KeyError: 1
. 如果您或任何人知道如何解决此错误,请留下回复以提供帮助。提前致谢 :)
解决方案
dic
正在返回一个 dict 对象,而不是一个列表 - 所以你不能使用索引来获取第一项。
你可以这样做
word = dic.meaning('book')
print(list(word.values())[0])
请注意,在 Python 和大多数其他语言中,计数从 0 开始。因此列表中的第一项是索引 0 而不是 1。
推荐阅读
- python - Urllib 返回 html 但没有结束段落标签
- angular - 在 Angular 模板中创建局部变量
- java - 如何使用 Selenium 和 JavaFX 禁用 ChromeDriver 的日志记录
- mysql - MySQL查询返回false,但正确执行
- java - Java SHA256 生成与 Python 中不同的哈希
- ios - Azure Active Directory Safari 重定向问题
- database - Sitecore.Context.Database 是 Web 但 GetItem() 为所有内容返回 null
- javascript - 访问作为参数传递的对象
- javascript - 用户输入时如何获取文本框文本的长度
- sql-server - MSSQL 在列中显示垂直数据