Python将内部字典附加到外部字典到列表中

时间:2020-05-22 11:01:25

标签: python json dictionary

我正在尝试将多个内部字典对象关联到一个外部字典中。最终添加到最终列表中。

我的代码:

from collections import defaultdict

data = {"events":[{"inner_1":"11111","inner_2":"10.90.1.14","inner_3":"Active","outer_1":"45038","outer_2":"No","outer_3":"No Time"},{"inner_1":"33333","inner_2":"10.22.1.35","inner_3":"No","outer_1":"38610","outer_2":"No","outer_3":"Some Time"},{"inner_1":"4444","inner_2":"10.22.1.11","inner_3":"Active","outer_1":"38610","outer_2":"No","outer_3":"Some Time"},{"inner_1":"22222","inner_2":"10.90.1.33","inner_3":"No","outer_1":"45038","outer_2":"No","outer_3":"Some Time"},{"inner_1":"55555","inner_2":"10.90.1.33","inner_3":"Active","outer_1":"45038","outer_2":"Active","outer_3":"Some Time"}]}

final_list = []
outer_dict = defaultdict(list)
inner_dict = defaultdict(list)
rows = data.get('events')
counter = 0
for row in rows:
    table_list = []
    o1 = str(row.get('outer_1'))
    o2 = str(row.get('outer_2'))
    o3 = str(row.get('outer_3'))
    i1 = str(row.get('inner_1'))
    i2 = str(row.get('inner_2'))
    i3 = str(row.get('inner_3'))

    inner_dict = {"inner_1":i1,"inner_2":i2,"inner_3":i3}

    if outer_dict.get("outer_1") != o1:
        outer_dict = {"outer_1":o1,"outer_2":o2,"outer_3":o3}
        outer_dict.setdefault("details", []).append(inner_dict)
        final_list.append(outer_dict)
    else:
        for i in final_list:
            if i.get("outer_1") == o1:
                i.setdefault("details", []).append(inner_dict)
print(final_list)

预期数据:

[
 {"outer_1":"45038",
  "outer_2":"No",
  "outer_3":"No Time",
  "details":[
             {"inner_3":"Active","inner_1":"11111","inner_2":"10.90.1.14"}, 
             {"inner_3":"Active","inner_1":"55555","inner_2":"10.90.1.33"}, 
             {"inner_3":"No","inner_1":"22222","inner_2":"10.90.1.33"}
  ]},
  {"outer_1":"38610",
   "outer_2":"No",
   "outer_3":"Some Time",
   "details":[{"inner_3":"No","inner_1":"33333","inner_2":"10.22.1.35"},
             {"inner_3":"Active","inner_1":"4444","inner_2":"10.22.1.11"}
  ]}
]

输出错误:

[
 {"outer_1":"45038",
  "outer_2":"No",
  "outer_3":"No Time",
  "details":[{"inner_3":"Active","inner_1":"11111","inner_2":"10.90.1.14"},
             {"inner_3":"Active","inner_1":"55555","inner_2":"10.90.1.33"}
   ]},
  {"outer_1":"38610",
   "outer_2":"No",
   "outer_3":"Some Time",
   "details":[{"inner_3":"No","inner_1":"33333","inner_2":"10.22.1.35"},
              {"inner_3":"Active","inner_1":"4444","inner_2":"10.22.1.11"}
   ]},
  {"outer_1":"45038",
   "outer_2":"No",
   "outer_3":"Some Time",
   "details":[{"inner_3":"No","inner_1":"22222","inner_2":"10.90.1.33"},
              {"inner_3":"Active","inner_1":"55555","inner_2":"10.90.1.33"}
   ]}
]

请帮助!

2 个答案:

答案 0 :(得分:3)

尝试一下:

from collections import defaultdict
data = {"events":[{"inner_1":"11111","inner_2":"10.90.1.14","inner_3":"Active","outer_1":"45038","outer_2":"No","outer_3":"No Time"},{"inner_1":"33333","inner_2":"10.22.1.35","inner_3":"No","outer_1":"38610","outer_2":"No","outer_3":"Some Time"},{"inner_1":"4444","inner_2":"10.22.1.11","inner_3":"Active","outer_1":"38610","outer_2":"No","outer_3":"Some Time"},{"inner_1":"22222","inner_2":"10.90.1.33","inner_3":"No","outer_1":"45038","outer_2":"No","outer_3":"Some Time"},{"inner_1":"55555","inner_2":"10.90.1.33","inner_3":"Active","outer_1":"45038","outer_2":"Active","outer_3":"Some Time"}]}

inner, outer = defaultdict(list), {}
for x in data['events']:
    inner[x['outer_1']].append({k: v for k, v in x.items() if 'inner_' in k}) 
    if x['outer_1'] not in outer:
        outer[x['outer_1']] = {k: v for k, v in x.items() if 'outer_' in k}

for k, v in outer.items():
    v['details'] = inner[k]

ans = list(outer.values())
print(ans)

输出

[
  {
    "outer_1": "45038",
    "outer_2": "No",
    "outer_3": "No Time",
    "details": [
      {
        "inner_1": "11111",
        "inner_2": "10.90.1.14",
        "inner_3": "Active"
      },
      {
        "inner_1": "22222",
        "inner_2": "10.90.1.33",
        "inner_3": "No"
      },
      {
        "inner_1": "55555",
        "inner_2": "10.90.1.33",
        "inner_3": "Active"
      }
    ]
  },
  {
    "outer_1": "38610",
    "outer_2": "No",
    "outer_3": "Some Time",
    "details": [
      {
        "inner_1": "33333",
        "inner_2": "10.22.1.35",
        "inner_3": "No"
      },
      {
        "inner_1": "4444",
        "inner_2": "10.22.1.11",
        "inner_3": "Active"
      }
    ]
  }
]

答案 1 :(得分:0)

我也尝试过一件事。我使用了if条件来减少for循环。

data = {"events":[{"inner_1":"11111","inner_2":"10.90.1.14","inner_3":"Active","outer_1":"45038","outer_2":"No","outer_3":"No Time"},{"inner_1":"33333","inner_2":"10.22.1.35","inner_3":"No","outer_1":"38610","outer_2":"No","outer_3":"Some Time"},{"inner_1":"4444","inner_2":"10.22.1.11","inner_3":"Active","outer_1":"38610","outer_2":"No","outer_3":"Some Time"},{"inner_1":"22222","inner_2":"10.90.1.33","inner_3":"No","outer_1":"45038","outer_2":"No","outer_3":"Some Time"},{"inner_1":"55555","inner_2":"10.90.1.33","inner_3":"Active","outer_1":"45038","outer_2":"Active","outer_3":"Some Time"}]}

final_list = []
rows = data.get('events')
counter = 0
insert = {}
for row in rows:
    table_list = []
    o1 = str(row.get('outer_1'))
    o2 = str(row.get('outer_2'))
    o3 = str(row.get('outer_3'))
    i1 = str(row.get('inner_1'))
    i2 = str(row.get('inner_2'))
    i3 = str(row.get('inner_3'))


    inner_dict = {"inner_1":i1,"inner_2":i2,"inner_3":i3}

    for item in final_list:
        if item.get("outer_1") == o1:
            item["details"].append(inner_dict)
            insert[o1] = True
    if not insert.get(o1):
        outer_dict = {"outer_1":o1,"outer_2":o2,"outer_3":o3}
        outer_dict["details"] = [inner_dict]
        final_list.append(outer_dict)
print(final_list)