gpt4 book ai didi

python - 如何合并包含相同键值的字典对象,并输出一个列表?

转载 作者:太空宇宙 更新时间:2023-11-04 01:54:48 25 4
gpt4 key购买 nike

我想添加包含相同键值的字典对象,并得到一个列表。我有一个功能,我这样做:

def arizakestirimi_func(self):
arizatahmin = []
...
record = {} //create a new dict
record["isyeri"]=key
record["mekanikarizatahminsuresi"]= arizatahminsüresi
arizatahmin.append(record);
...
record = {} //create a new dict
record["isyeri"]=key //key can be same
record["elektrikarizatahminsuresi"]= arizatahminsüresi //this is different key value
arizatahmin.append(record);
...
record = {} //create a new dict
record["isyeri"]=key //key can be same
record["kesintilerarizatahminsuresi"]= arizatahminsüresi //this is different key value

arizatahmin.append(record);

#Return value as a HttpResponse.
return HttpResponse(json.dumps({'arizaTahminSuresi': arizatahmin},indent=2), content_type="application/json")

我的输出是这样的json:

{
"arizaTahminSuresi": [
{
"isyeri": "15400005",//The same key
"mekanikarizatahminsuresi": 38.95637287225691
},

{
"isyeri": "15400005",//The same key
"elektrikarizatahminsuresi": 427.9069449086019
},

"isyeri": "15400005",//The same key
"kesintilerarizatahminsuresi": 4.882768280126584
}

]
}

我希望输出如下。我该怎么做?

{
"arizaTahminSuresi": [
{
"isyeri": "15400005",
"mekanikarizatahminsuresi": 38.95637287225691
"elektrikarizatahminsuresi": 427.9069449086019
"kesintilerarizatahminsuresi": 4.882768280126584
}
]
}

最佳答案

您可以使用 groupby 获取所有匹配的字典,然后使用 update 取消它们,如下所示:

from itertools import groupby
from operator import itemgetter
from pprint import pprint

original = {
"arizaTahminSuresi": [
{
"isyeri": "15400005",
"mekanikarizatahminsuresi": 38.95637287225691
},
{
"isyeri": "15400003",
"mekanikarizatahminsuresi": 312.05942081303965
},
{
"isyeri": "15400002",
"mekanikarizatahminsuresi": 773.600387779049
},
{
"isyeri": "15400004",
"mekanikarizatahminsuresi": 275.25620818317475
},
{
"isyeri": "15400001",
"mekanikarizatahminsuresi": 9.032135504089638
},
{
"isyeri": "15400004",
"elektrikarizatahminsuresi": 105.3605215202133
},
{
"isyeri": "15400003",
"elektrikarizatahminsuresi": 463.8990083193239
},
{
"isyeri": "15400001",
"elektrikarizatahminsuresi": 49.71052726257949
},
{
"isyeri": "15400005",
"elektrikarizatahminsuresi": 427.9069449086019
},
{
"isyeri": "15400002",
"elektrikarizatahminsuresi": 25.78649617892279
},
{
"isyeri": "15400004",
"kesintilerarizatahminsuresi": 67.41996630760787
},
{
"isyeri": "15400005",
"kesintilerarizatahminsuresi": 4.882768280126584
},
{
"isyeri": "15400002",
"kesintilerarizatahminsuresi": 58.96635620693996
},
{
"isyeri": "15400001",
"kesintilerarizatahminsuresi": 357.28675445627414
},
{
"isyeri": "15400003",
"kesintilerarizatahminsuresi": 434.16131082294305
}

]
}

grouped_subdicts = groupby(sorted(original["arizaTahminSuresi"], key=itemgetter("isyeri")), itemgetter("isyeri"))

result = []
for k,g in grouped_subdicts:
d = {}
for sub_d in g:
d.update(sub_d)
result.append(d)

original["arizaTahminSuresi"] = result

pprint(original)

关于python - 如何合并包含相同键值的字典对象,并输出一个列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57034608/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com