python - 如何访问嵌套字典的内部值
问题描述
我有以下嵌套字典,
Data = [{'seriesId': 'Food and Beverage',
'forecastPoint': '2020-08-26T00:00:00Z',
'rowId': 35,
'timestamp': '2020-08-27T00:00:00.000000Z',
'predictionValues': [{'value': 32.5133330947, 'label': 'Volume (actual)'}],
'forecastDistance': 1,
'prediction': 32.5133330947},
{'seriesId': 'Food and Beverage',
'forecastPoint': '2020-08-26T00:00:00Z',
'rowId': 36,
'timestamp': '2020-08-28T00:00:00.000000Z',
'predictionValues': [{'value': 30.2438893873, 'label': 'Volume (actual)'}],
'forecastDistance': 2,
'prediction': 30.2438893873}]
但是,我想将该字典带入以下表单字典,我只想访问value
frompredictionValues
并按原样获取其他键。
预期输出应该是字典
Data = [{'seriesId': 'Food and Beverage',
'forecastPoint': '2020-08-26T00:00:00Z',
'rowId': 35,
'timestamp': '2020-08-27T00:00:00.000000Z',
'value': 32.5133330947,
'forecastDistance': 1,
'prediction': 32.5133330947},
{'seriesId': 'Food and Beverage',
'forecastPoint': '2020-08-26T00:00:00Z',
'rowId': 36,
'timestamp': '2020-08-28T00:00:00.000000Z',
'value': 30.2438893873,
'forecastDistance': 2,
'prediction': 30.2438893873}]
解决方案
尝试这个
for d in Data:
d.update({'value': d.pop('predictionValues')[0].pop('value')})
推荐阅读
- r - R - 选择案例,以便变量的平均值是某个给定的数字
- html - 元素间隙的弹性盒空间均匀选择器?
- c# - 以波浪号开头的 Blazor 参考的解释是什么?“~/(...)”
- c# - Neo4j Driver C# 展开对象列表
- php - 使用通用键搜索多维 JSON 数组的值 - php
- jquery - 启动stream2时如何停止stream1
- r - 是否有 R 函数可以快速将文本转换为日期?
- excel - VBA slicercache 导出 PDF 循环分解 Excel
- flutter - 如何以正确的方式从 SharedPreferences 获取数据
- c# - 我应该在我的 Task.Run() 中使用 await 吗?