Python将嵌套的字典列表合并到单个字典列表中

时间:2018-03-19 01:10:04

标签: python

我想将嵌套的字典列表合并到python 2.6中字典的单个列表中, 示例数据 - 这里只给出了数千次迭代的两次迭代。

INPUTJSON=[
 {'EXCEPTIONS': 
            [
              {'LASTOCCURED': '2018-03-12 12:11:23', 'COUNT': 25, 'NAME': 'CLFRW0134W'}, 
              {'LASTOCCURED': '2018-03-12 12:11:42', 'COUNT': 10, 'NAME': 'SRV0145GH'}
            ], 
  'JVM_NAME': 'TestiingAWS01', 'GCCOUNT': 10},

 {'EXCEPTIONS': 
           [
              {'LASTOCCURED': '2018-03-13 12:14:23', 'COUNT': 25, 'NAME': 'CLFRW0134W'}, 
              {'LASTOCCURED': '2018-03-18 12:55:23', 'COUNT': 10, 'NAME': 'SRV0145GH'}
           ], 
  'JVM_NAME': 'QAAWS02', 'GCCOUNT': 10}
]

预期结果:

[
 {'JVM_NAME':'TestiingAWS01','GCCOUNT':10, 'LASTOCCURED': '2018-03-12 12:11:23', 'COUNT': 25, 'NAME': 'CLFRW0134W'}, {'LASTOCCURED': '2018-03-12 12:11:42', 'COUNT': 10, 'NAME': 'SRV0145GH', 'JVM_NAME':'TestiingAWS01','GCCOUNT':10},
 {'JVM_NAME': 'QAAWS02', 'GCCOUNT': 10, 'LASTOCCURED': '2018-03-13 12:14:23', 'COUNT': 25, 'NAME': 'CLFRW0134W'}, {'LASTOCCURED': '2018-03-18 12:55:23', 'COUNT': 10, 'NAME': 'SRV0145GH', 'JVM_NAME': 'QAAWS02', 'GCCOUNT': 10}
]

请求专家帮助我实现这一目标,以便我可以轻松地将最终数据处理为sqlite。

更新:谢谢所有专家提供快速解决方案,从这里的所有答案中整合出来,我通过避免使用硬编码键来获得我的数据的工作代码" (因为在每次迭代时会有40个键)在python 2.6上。

def merge_two_dicts(x, y):
    """Given two dicts, merge them into a new dict as a shallow copy."""
    z = x.copy()
    z.update(y)
    return z

resultlist =[]
for i,v in enumerate(INPUTJSON):
    EXCEPTIONS = v["EXCEPTIONS"]
    del v["EXCEPTIONS"]
    for j,val in enumerate(EXCEPTIONS):
        resultlist.append(merge_two_dicts(EXCEPTIONS[j],INPUTJSON[i]))

print resultlist
  

可以使用lambda在理解列表中编译吗?

4 个答案:

答案 0 :(得分:2)

这是一种方式。

lst = [{**{'JVM_NAME': i['JVM_NAME'], 'GCCOUNT': i['GCCOUNT'], **w}} \
       for i in INPUTJSON for w in i['EXCEPTIONS']]

有关**语法,请参阅How to merge two dictionaries in a single expression?

<强>结果

[{'COUNT': 25,
  'GCCOUNT': 10,
  'JVM_NAME': 'TestiingAWS01',
  'LASTOCCURED': '2018-03-12 12:11:23',
  'NAME': 'CLFRW0134W'},
 {'COUNT': 10,
  'GCCOUNT': 10,
  'JVM_NAME': 'TestiingAWS01',
  'LASTOCCURED': '2018-03-12 12:11:42',
  'NAME': 'SRV0145GH'},
 {'COUNT': 25,
  'GCCOUNT': 10,
  'JVM_NAME': 'QAAWS02',
  'LASTOCCURED': '2018-03-13 12:14:23',
  'NAME': 'CLFRW0134W'},
 {'COUNT': 10,
  'GCCOUNT': 10,
  'JVM_NAME': 'QAAWS02',
  'LASTOCCURED': '2018-03-18 12:55:23',
  'NAME': 'SRV0145GH'}]

答案 1 :(得分:1)

你可以试试这个:

INPUTJSON=[
   {'EXCEPTIONS': 
        [
          {'LASTOCCURED': '2018-03-12 12:11:23', 'COUNT': 25, 'NAME': 
   'CLFRW0134W'}, 
          {'LASTOCCURED': '2018-03-12 12:11:42', 'COUNT': 10, 'NAME': 
  'SRV0145GH'}
        ], 
   'JVM_NAME': 'TestiingAWS01', 'GCCOUNT': 10},

  {'EXCEPTIONS': 
       [
          {'LASTOCCURED': '2018-03-13 12:14:23', 'COUNT': 25, 'NAME': 'CLFRW0134W'}, 
          {'LASTOCCURED': '2018-03-18 12:55:23', 'COUNT': 10, 'NAME': 'SRV0145GH'}
       ], 
    'JVM_NAME': 'QAAWS02', 'GCCOUNT': 10}
]
new_result = [i for b in [[dict([('JVM_NAME', i['JVM_NAME']), ('GCCOUNT', i['GCCOUNT'])]+b.items()) for b in i['EXCEPTIONS']] for i in INPUTJSON] for i in b]

输出:

[{'LASTOCCURED': '2018-03-12 12:11:23', 'JVM_NAME': 'TestiingAWS01', 'GCCOUNT': 10, 'COUNT': 25, 'NAME': 'CLFRW0134W'}, {'LASTOCCURED': '2018-03-12 12:11:42', 'JVM_NAME': 'TestiingAWS01', 'GCCOUNT': 10, 'COUNT': 10, 'NAME': 'SRV0145GH'}, {'LASTOCCURED': '2018-03-13 12:14:23', 'JVM_NAME': 'QAAWS02', 'GCCOUNT': 10, 'COUNT': 25, 'NAME': 'CLFRW0134W'}, {'LASTOCCURED': '2018-03-18 12:55:23', 'JVM_NAME': 'QAAWS02', 'GCCOUNT': 10, 'COUNT': 10, 'NAME': 'SRV0145GH'}]

但请注意,在使用解包时,Python3中的这个问题更简单:

final_result = [i for b in [[{**{a:b for a, b in i.items() if a != 'EXCEPTIONS'}, **c} for c in i['EXCEPTIONS']] for i in INPUTJSON] for i in b]

输出:

[{'LASTOCCURED': '2018-03-12 12:11:23', 'JVM_NAME': 'TestiingAWS01', 'GCCOUNT': 10, 'COUNT': 25, 'NAME': 'CLFRW0134W'}, {'LASTOCCURED': '2018-03-12 12:11:42', 'JVM_NAME': 'TestiingAWS01', 'GCCOUNT': 10, 'COUNT': 10, 'NAME': 'SRV0145GH'}, {'LASTOCCURED': '2018-03-13 12:14:23', 'JVM_NAME': 'QAAWS02', 'GCCOUNT': 10, 'COUNT': 25, 'NAME': 'CLFRW0134W'}, {'LASTOCCURED': '2018-03-18 12:55:23', 'JVM_NAME': 'QAAWS02', 'GCCOUNT': 10, 'COUNT': 10, 'NAME': 'SRV0145GH'}]

答案 2 :(得分:1)

data = list()
for item in INPUTJSON:
    EXCEPTIONS = item["EXCEPTIONS"]
    del item["EXCEPTIONS"]
    for ex in EXCEPTIONS:
        tmp = dict()
        tmp.update(ex)    
        tmp.update(item)
        data.append(tmp)
print data

答案 3 :(得分:1)

您可以尝试:

final_one=[]
for i in data:
    final=[]
    temp={}
    for m,n in i.items():
        if not isinstance(n,list):
            temp[m]=n
        else:
            final+=n
    for h in final:
        h.update(temp)
    final_one+=final
print(final_one)

输出:

[{'COUNT': 25, 'NAME': 'CLFRW0134W', 'LASTOCCURED': '2018-03-12 12:11:23', 'JVM_NAME': 'TestiingAWS01', 'GCCOUNT': 10}, {'COUNT': 10, 'NAME': 'SRV0145GH', 'LASTOCCURED': '2018-03-12 12:11:42', 'JVM_NAME': 'TestiingAWS01', 'GCCOUNT': 10}, {'COUNT': 25, 'NAME': 'CLFRW0134W', 'LASTOCCURED': '2018-03-13 12:14:23', 'JVM_NAME': 'QAAWS02', 'GCCOUNT': 10}, {'COUNT': 10, 'NAME': 'SRV0145GH', 'LASTOCCURED': '2018-03-18 12:55:23', 'JVM_NAME': 'QAAWS02', 'GCCOUNT': 10}]