在python中将2个dicts与common元素合并

时间:2015-09-04 20:09:27

标签: python list dictionary

我有一本字典如下:

{"states":[{"status": "BV"}, {"status": "CORR"}]}   

很明显,它不是一个简单的字典。它是一个列表,包含字典的列表。

我有另一本字典如下:

{
"all_diseases": [
    {
        "status": "BV", 
        "median": 240.0,
        "disease_name": "Lupus"
    }, 
    {
        "status": "BV", 
        "median": 270.0,
        "disease_name": "Pulmonary Arterial Hypertension"
    }, 
    {
        "status": "CORR", 
        "median": 480.0,
        "disease_name": "Lupus"
    }, 
    {
        "status": "CORR", 
        "median": 600.0,
        "disease_name": "Pulmonary Arterial Hypertension"
    }, 
  ]
}

这也是列表的列表,列表包含dicts。 status键是这里的常用元素。如何合并这两个结构以得到这样的输出:

{
"output": 
        [
          {
            "status":"BV",
            "data":
                    [
                      {
                        "median": 240.0,
                        "disease_name": "Lupus"
                      },
                      {
                        "median": 270.0, 
                        "disease_name": "Pulmonary Arterial Hypertension"
                      }
                    ]        
          },               
          {
            "status":"CORR",
            "data":
                    [
                      {
                        "median": 480.0,
                        "disease_name": "Lupus"
                      },
                      {
                        "median": 600.0, 
                        "disease_name": "Rheumatology"
                      }
                    ]        
           }
         ]
}

3 个答案:

答案 0 :(得分:1)

你基本上是按州分组;你真的不需要这里的第一本字典:

states = {}
for disease in seconddict['all_diseases']:
    state = disease.pop('status')
    states.setdefault(state, []).append(disease)

之后您可以创建输出:

output = {'output': [
    {'status': status, 'data': diseases}
    for status, diseases in states.iteritems()]}

演示:

>>> states = {}
>>> for disease in seconddict['all_diseases']:
...     state = disease.pop('status')
...     states.setdefault(state, []).append(disease)
... 
>>> states
{'BV': [{'disease_name': 'Lupus', 'median': 240.0}, {'disease_name': 'Pulmonary Arterial Hypertension', 'median': 270.0}], 'CORR': [{'disease_name': 'Lupus', 'median': 480.0}, {'disease_name': 'Pulmonary Arterial Hypertension', 'median': 600.0}]}
>>> from pprint import pprint
>>> output = {'output': [
...     {'status': status, 'data': diseases}
...     for status, diseases in states.iteritems()]}
>>> pprint(output)
{'output': [{'data': [{'disease_name': 'Lupus', 'median': 240.0},
                      {'disease_name': 'Pulmonary Arterial Hypertension',
                       'median': 270.0}],
             'status': 'BV'},
            {'data': [{'disease_name': 'Lupus', 'median': 480.0},
                      {'disease_name': 'Pulmonary Arterial Hypertension',
                       'median': 600.0}],
             'status': 'CORR'}]}

答案 1 :(得分:1)

itertools.groupby是您的朋友,您需要实施关键功能标准并将其作为参数传递。

您可以这样使用:

    XDG.CommitEdit();

答案 2 :(得分:-1)

states = {"states":[{"status": "BV"}, {"status": "CORR"}]}   
status = [x.get('status') for x in states.get('states', [])]

diseases = {
"all_diseases": [
    {
        "status": "BV", 
        "median": 240.0,
        "disease_name": "Lupus"
    }, 
    {
        "status": "BV", 
        "median": 270.0,
        "disease_name": "Pulmonary Arterial Hypertension"
    }, 
    {
        "status": "CORR", 
        "median": 480.0,
        "disease_name": "Lupus"
    }, 
    {
        "status": "CORR", 
        "median": 600.0,
        "disease_name": "Pulmonary Arterial Hypertension"
    }, 
  ]
}

print [{"status": s, "data":[{"median": x.get("median"), "disease_name": x.get("disease_name")} for x in diseases.get("all_diseases", []) if x.get("status") == s]} for s in status]