我有一个具有以下结构的列表:
data = [[
{
"id": 713,
"prediction": 4.8,
"confidence": [
{"percentile": "75", "lower": 4.8, "upper": 5.7}
],
},
{
"id": 714,
"prediction": 4.93,
"confidence": [
{"percentile": "75", "lower": 4.9, "upper": 5.7}
],
},
],
[
{
"id": 713,
"prediction": 5.8,
"confidence": [
{"percentile": "75", "lower": 4.2, "upper": 6.7}
],
},
{
"id": 714,
"prediction": 2.93,
"confidence": [
{"percentile": "75", "lower": 1.9, "upper": 3.7}
],
},
]]
这里有一个包含两个列表的列表,但它也可能包含更多的列表。每个列表都包含一个带有id和置信区间的预测,这些置信区间在另一个带有字典的列表中。
我需要将这些列表合并,以便对于每个id只有一个字典,其中包含数值的平均值。
我尝试过搜索,但没有找到与此嵌套结构匹配的答案。
期望的输出应该如下所示:
merged_data = [
{
"id": 713,
"prediction": 5.3,
"confidence": [
{"percentile": "75", "lower": 4.5, "upper": 6.2}
],
},
{
"id": 714,
"prediction": 3.93,
"confidence": [
{"percentile": "75", "lower": 3.4, "upper": 4.7}
],
},
]
for
循环可以实现这个。请展示您尝试过的代码。 - roganjosh