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中的嵌套字典中获取所有值
基础教程推荐
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
