从python中的嵌套字典中获取所有值 [英] Get all values from nested dictionaries in python

查看:26
本文介绍了从python中的嵌套字典中获取所有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些字典的字典,像这样:

I have some dictionaries of dictionaries, like this:

a['b']['c']['d']['answer'] = answer1
a['b']['c']['e']['answer'] = answer2
a['b']['c']['f']['answer'] = answer3
....
a['b']['c']['d']['conf'] = conf1
a['b']['c']['e']['conf'] = conf2
a['b']['c']['f']['conf'] = conf3

是否有一种快速的方法可以获取第三级 (d,e,f) 上所有元素的所有答案的值列表?

Is there a fast way to get a list of values of all answers for all elements at the third level (d,e,f)?

特别是我想知道是否有任何实现通配符的机制(例如,a['b']['c']['*']['answer'].values()

Specifically I'd like to know if there's any mechanism implementing a wildcard (e.g., a['b']['c']['*']['answer'].values()

更新到目前为止,我发现的最快方法是:

update The fastest way I've found till now is:

[x['answer'] for x in a['b']['c'].values()]

推荐答案

只是为了回答这个话题,从我问题的更新状态"复制我的解决方案:

Just to give an answer to this topic, copying my solution from the "updating status" of my question:

[x['answer'] for x in a['b']['c'].values()]

希望能帮到你.

这篇关于从python中的嵌套字典中获取所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆