[英]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.