在特定关键字的嵌套字典中获取值的优雅方式? [英] Elegant way to get values in nested dictionaries for a specific key?
本文介绍了在特定关键字的嵌套字典中获取值的优雅方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
A
元素: D [0] ['detLog'] [n] ['A']
其中n从0到长度的 detLog
...在Matlab中,我可以使用这样的东西:
D [0] ['detLog'] [:] ['A']
:
意思是所有元素。
在Python中有什么类似的东西?
解决方案
列表理解:
$在D [0] ['detLog']中,b $ b
[d ['A'] d]
对于使用Python的科学计算,您可能还需要查看 NumPy 和 SciPy ,特别是用于Matlab用户的NumPy 文档。
I have a nested dictionary in Python. I can access the A
element like this:
D[0]['detLog'][n]['A']
where n is from 0 to the length of the detLog
... In Matlab I could use something like this:
D[0]['detLog'][:]['A']
:
meaning "for all elements".
Is there something similar in Python?
解决方案
Yes, use a list comprehension:
[d['A'] for d in D[0]['detLog']]
For scientific computing with Python, you may also want to look into NumPy and SciPy, specifically the NumPy for Matlab users documentation.
这篇关于在特定关键字的嵌套字典中获取值的优雅方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文