如何添加列表中包含相同键值的dict对象?

时间:2019-07-15 06:56:37

标签: python list dictionary

我想添加在列表上包含相同键值的dict对象。我有一个功能,我愿意:

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
    }   
  ]
}

谢谢。

2 个答案:

答案 0 :(得分:2)

您可以使用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) 取消对它们的匹配,如下所示:

{{1}}

答案 1 :(得分:0)

将后面的字典与上一个字典合并。

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[0]= {**arizatahmin[0], **record};
    ...
    record = {} //create a new dict
    record["isyeri"]=key //key can be same 
    record["kesintilerarizatahminsuresi"]= arizatahminsüresi //this is different key value

    arizatahmin[0]= {**arizatahmin[0], **record};

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