【发布时间】:2016-03-04 10:02:31
【问题描述】:
我有一个字典,每个键在一个列表中有多个值。 任务是:
- 检测给定单词是否在字典值中
- 如果为真,则从字典中返回相应的键
任务 1 是通过使用 if 条件来实现的:
if (word in dictionary[topics] for topics in dictionary.keys())
我想在 if 条件评估为 True 时获取主题。像
if (word in dictionary[topics] for topics in dictionary.keys()):
print topics
【问题讨论】:
-
看看“列表推导式”(俗气的教程here)
-
感谢@PaulRichter,这个教程非常有用。
标签: python if-statement for-loop dictionary