【发布时间】:2018-08-03 08:33:23
【问题描述】:
我是 python 新手,我必须从 dict 中提取 subdict。我有一本这样的字典
{'stat': u'AS', 'vyska': 3.72, 'stanice': u'AQW00061705', 'mesic': 1, 'teplotaC': 26.88}
我希望 python 打印value of 'stat' and 'teplotaC',但前提是'mesic' in (6,7,8)。所以在上面的例子中,python 不会返回任何内容,但是如果字典看起来像这样:
{'stat': u'AS', 'vyska': 3.72, 'stanice': u'AQW00061705', 'mesic': 8, 'teplotaC': 26.88}
它会返回AS 26.88 有什么建议吗?
我知道一些如何在 python 中使用 dicts 的基础知识,比如如何通过键访问一些值等。但是我的 dicts 比这个例子大得多,我有 5000 个 dicts,我必须找出每个其中之一,如果"mesic" in (6,7,8),如果是这样,我必须返回"stat" and "teplotaC"。所以我需要一些函数来输入字典,当"mesic" 的条件为True 时,它将返回stat 和teplotaC。
【问题讨论】:
-
建议:阅读一些有关 Python 的书籍。使用 dicts 是非常基础的知识。
-
我知道一些基础知识,所以我将添加有关我的基础知识的信息来提问。但是我的字典比这个例子大得多,我有 5000 个字典,我必须在每个字典中找出,如果 (6,7,8) 中的“mesic”,如果是,我必须返回“ stat" 和 "teplotaC" 。所以我需要一些函数来输入字典,当“mesic”的条件为 True 时,它将返回 stat 和 teplotaC。
标签: python dictionary subset