Python 2.7 组合包含DICT列表的DICT列表中的值
这超出了我的舒适范围,我甚至不确定我是否能很好地描述它。我有一个文件,它是一个目录列表,其中包含另一个目录列表。数据结构摘录如下:Python 2.7 组合包含DICT列表的DICT列表中的值,python-2.7,dictionary,Python 2.7,Dictionary,这超出了我的舒适范围,我甚至不确定我是否能很好地描述它。我有一个文件,它是一个目录列表,其中包含另一个目录列表。数据结构摘录如下: j_traffic = [ { "timePeriod": "2017-08-04T15:20:00.000+0000", "applicationTrafficPerApplication": [ { "applicationId": 39, "applicationNam
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))
有人要吗
谢谢 这是一种按照你的要求去做的可能性:
# 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循环中的最后一行是由于这篇文章
您想对输出进行排序吗?这是一种按您的要求进行排序的可能性:
# 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循环中的最后一行是由于这篇文章
您想对输出进行排序吗?这里有一个简单明了的代码,用于处理j_流量列表并获得预期的输出
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)
下面是一个简单明了的代码,用于处理j_流量列表并获得预期的输出
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)