Get all values from nested dictionaries in python(从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中的嵌套字典中获取所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从python中的嵌套字典中获取所有值
- 计算测试数量的Python单元测试 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01