将值组合在包含dicts列表的dicts列表中

时间:2017-08-24 17:15:31

标签: python-2.7 dictionary

这远远超出了我的舒适范围,我甚至不确定我是否可以很好地描述它。我有一个文件,其中包含另一个dicts列表的dicts列表。下面是数据结构的摘录:

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”值作为键来创建新的dict,并且值是关键字“trafficInboundBps”的所有值的总和,如下所示:

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

我已经尝试了我在这里找到的建议,但无法解决如何使用以下内容解析嵌套级别: inboundTraffic = dict.fromkeys(set()。union(* j_traffic))

任何参赛者?

谢谢!

2 个答案:

答案 0 :(得分:0)

这是你提出要求的一种可能性:

# 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

我还在学习pythonic的做事方式,所以我打赌有一个更短更合适的解决方案 - 但这确实可以解决问题。

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

您希望输出排序吗?

答案 1 :(得分:0)

这是一个简单而简单的代码,用于处理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)