结合列表中的值包含一系列的词典

问题描述:

这是超出我的舒适范围,我甚至不确定我可以描述得足够好。我有一个文件,它是一个包含另一个字典列表的字典列表。数据结构的摘录如下:结合列表中的值包含一系列的词典

j_traffic = 
[ 
    { 
    "timePeriod": "2017-08-04T15:20:00.000+0000", 
    "applicationTrafficPerApplication": [ 
     { 
     "applicationId": 39, 
     "applicationName": "HTTP", 
     "trafficInboundBps": 148760, 
     "trafficOutboundBps": 5673493, 
     "trafficWithinBps": 0 
     }, 
     { 
     "applicationId": 41, 
     "applicationName": "HTTPS", 
     "trafficInboundBps": 16805, 
     "trafficOutboundBps": 546937, 
     "trafficWithinBps": 0 
     } 
     ] 
    }, 
    { 
    "timePeriod": "2017-08-04T15:15:00.000+0000", 
    "applicationTrafficPerApplication": [ 
     { 
     "applicationId": 39, 
     "applicationName": "HTTP", 
     "trafficInboundBps": 157569, 
     "trafficOutboundBps": 5769206, 
     "trafficWithinBps": 0 
     }, 
     { 
     "applicationId": 41, 
     "applicationName": "HTTPS", 
     "trafficInboundBps": 17454, 
     "trafficOutboundBps": 590421, 
     "trafficWithinBps": 0 
     }, 
     { 
     "applicationId": 44, 
     "applicationName": "DNS", 
     "trafficInboundBps": 18218, 
     "trafficOutboundBps": 13683, 
     "trafficWithinBps": 0 
     }, 
     { 
     "applicationId": 45, 
     "applicationName": "SNMP", 
     "trafficInboundBps": 14, 
     "trafficOutboundBps": 0, 
     "trafficWithinBps": 0 
     } 
     ] 
    }, 
    { 
    "timePeriod": "2017-08-04T15:05:00.000+0000", 
    "applicationTrafficPerApplication": [ 
     { 
     "applicationId": 39, 
     "applicationName": "HTTP", 
     "trafficInboundBps": 139897, 
     "trafficOutboundBps": 5073320, 
     "trafficWithinBps": 0 
     }, 
     { 
     "applicationId": 41, 
     "applicationName": "HTTPS", 
     "trafficInboundBps": 22592, 
     "trafficOutboundBps": 457962, 
     "trafficWithinBps": 0 
     }, 
     { 
     "applicationId": 44, 
     "applicationName": "DNS", 
     "trafficInboundBps": 19903, 
     "trafficOutboundBps": 14033, 
     "trafficWithinBps": 0 
     } 
     ] 
    } 
] 

我想明白,我怎么能使用“的applicationName”值作为密钥,来创建一个新的字典和值是关键“trafficInboundBps”的所有值的总和这应该是这样的:

inboundTraffic = { “HTTP”:446316, “HTTPS”:56581, “DNS”:38121, “SNMP”:14}

我试过的建议,我发现就在这里但不能包装我的头周围如何解析嵌套的级别与以下内容: inboundTraffic = dict.fromkeys(set()。union(* j_traffic))

任何接受者?

谢谢!

下面是一个简单而简单的代码,用于处理j_traffic列表并获取预期的输出。

output = dict() 
# iterate over the list of outer dicts 
for outer_dict in j_traffic: 
    # grab the list assigned to key applicationTrafficPerApplication 
    application_traffic_per_application = outer_dict['applicationTrafficPerApplication'] 
    # iterate over the list of inner dicts 
    for inner_dict in application_traffic_per_application: 
     application_name = inner_dict['applicationName'] 
     traffic_inbound_bps = inner_dict['trafficInboundBps'] 
     if application_name in output: 
      output[application_name] += int(traffic_inbound_bps) 
     else: 
      output[application_name] = int(traffic_inbound_bps) 

print(output) 

这是一个可能性,做什么你问:

# Extract outer dictionaries as a list 
lst = [s["applicationTrafficPerApplication"] for s in j_traffic] 

# Turn first element of lst into a dictionary 
inboundTraffic={s2["applicationName"]: s2["trafficInboundBps"] for s2 in lst[0]} 
# Process remaining elements - combine and add 
for comp in lst[1:]: 
    temp = {s2["applicationName"]: s2["trafficInboundBps"] for s2 in comp} 
    # This turns both dictionaries into sets, selects all elements 
    # (I assume that's why it's using sets - to have access to all), 
    # then adds the resepective elements - 0 in .get(k,0) signifies that 
    # "0" will be added if particular element doesn't exist in the second set/dictionary 
    inboundTraffic = {k: inboundTraffic.get(k,0) + temp.get(k,0) for k in set(inboundTraffic) | set(temp)} 

print inboundTraffic 

我还在学习做事情,所以我敢肯定有一个更短和更妥善的解决办法的Python的方式 - 但是这确实是不诀窍。

for循环的最后一行是由于这篇文章Merge and sum of two dictionaries

你想让你的输出排序吗?