简体   繁体   English

如何通过嵌套在列表中的键从字典中获取元素?

[英]How can I get elements from dictionary by key nested in a list?

I have construction like this: 我有这样的建设:

[{
    "meta": {
        "foo": 1, 
        "bar": "string", 
        "baz": "string2"},
    "data": [
        {"id": "1", "quant": 2, "price": 3.14},
        {"id": "2", "quant": 1, "price": 6.66}
    ]
 },
...
]

How can I get elements foo, bar from meta and quant, price from data? 如何获取元素foo,条形图和量化的条形图,数据的价格?

Welcome to SO, We are not code writing services so please post your effort next time you ask question. 欢迎使用SO,我们不是代码编写服务,因此,下次您提出问题时,请发表您的努力。 Refer to the documentation for more information on python Data Sructures. 有关python数据结构的更多信息,请参考文档

lists are accessed through index , and Dictionaries are accessed through `keys' lists通过index访问,而Dictionaries通过`keys'访问

Like _list[index] and _dict['key'] With that basics lets move forward. _list[index]_dict['key'] ,让我们继续前进。

l= [{ "meta":{ "foo":1, "bar":"string", "baz":"string2"}, "data":[ {"id":"1", "quant":2, "price":3.14}, {"id":"2", "quant":1, "price":6.66}] } ]

>>> l[0]['meta']['foo']
1
>>> l[0]['meta']['bar']
'string'

>>> l[0]['data'][0]['price']
3.14
>>> l[0]['data'][0]['quant']
2
>>> 

>>> l[0]['data'][1]['quant']
1
>>> l[0]['data'][1]['price']
6.66
>>> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从嵌套在列表中的字典中获取最大值 - How can I get the highest value from a dictionary nested in a list 如何获取键值对以从嵌套字典创建字典? - How can I get the key value pair to create a dictionary from a nested dictionary? 如何创建嵌套字典键并从命名空间键值对列表中为其分配值? - how can I create nested dictionary keys and assign them values from a list of namespaced key value pairs? 如何将列表中的元素类型存储在 Python 的字典中? - How can I store the types of elements from a list in a dictionary in Python? 如何从嵌套列表和字典中读取字典键和值 - how to read dictionary key and values from a nested list and dictionary 获取按字典键排序的字典元素列表 - Get list of dictionary elements ordered by dictionary key 如何将列表中的值引用到字典键值? - How can I reference a value from a list to the dictionary key value? 如何使用循环列表中的键将值添加到字典中 - How can I add the value into dictionary with the key from looping the list 如何从嵌套字典中获取键? - How can I get the keys from a nested dictionary? 我怎样才能得到一本字典,其中包含给定列表中句子中的一个词作为键和它在给定列表中出现的列表 - How can I get a dictionary that contains a word from a sentence in a given list as key and list of it's occurences in that given list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM