比较和组合dictonaries

时间:2018-02-08 00:35:34

标签: python

我有一个列表,其中包含不同数量的词典。

我需要在输入中循环每个字典并比较键值's_source_zone''s_destination_zone'和'Services'。如果这些键值匹配,我需要将它们(键源IP和目标IP)组合到存储在result []中的一个字典中。如果它们不匹配,则需要将输入字典添加到结果中。

基本上将所有“匹配”词典合并为一个。见下面的例子

    result = []
    input_ = [{'s_logical_system': 'logical_1', 's_virtual_router': 'vr_1', 's_matched_route': '10.0.0.0/8', 's_source_zone': 'zone_cccc_1', 's_destination_zone': 'zone_bbbb_1', 'Services': 80, 'Source IP': '10.10.10.10', 'Destination IP': '10.20.20.20'}, {'s_logical_system': 'logical_1', 's_virtual_router': 'vr_1', 's_matched_route': '10.0.0.0/8', 's_source_zone': 'zone_cccc_1', 's_destination_zone': 'zone_bbbb_1', 'Services': 80, 'Source IP': '10.40.10.10', 'Destination IP': '10.10.50.20'}, {'s_logical_system': 'logical_3', 's_virtual_router': 'vr_2', 's_matched_route': '10.0.0.0/8', 's_source_zone': 'zone_zzzz_1', 's_destination_zone': 'zone_rrrr_1', 'Services': 443, 'Source IP': '10.10.10.10', 'Destination IP': '10.20.20.20'}]

    if 's_source_zone', 's_destination_zone' and 'Services' all have the same values, combine the keys "Source IP" and "Destination IP" into one dictionary appended to result[]. See below;

    result = [{'s_logical_system': 'logical_1', 's_virtual_router': 'vr_1', 's_matched_route': '10.0.0.0/8', 's_source_zone': 'zone_cccc_1', 's_destination_zone': 'zone_bbbb_1', 'Services': 80, 'Source IP': '10.10.10.10, 10.40.10.10', 'Destination IP': '10.20.20.20, 10.10.50.20'}, {'s_logical_system': 'logical_3', 's_virtual_router': 'vr_2', 's_matched_route': '10.0.0.0/8', 's_source_zone': 'zone_zzzz_1', 's_destination_zone': 'zone_rrrr_1', 'Services': 443, 'Source IP': '10.10.10.10', 'Destination IP': '10.20.20.20'}]

if match not found. Append the entire dictionary to result. result.append(input_[x])

1 个答案:

答案 0 :(得分:1)

您可以使用itertools.groupby执行此操作:

from itertools import groupby

match_on = ['s_source_zone', 's_destination_zone', 'Services',
            's_logical_system', 's_virtual_router']
groupfunc = lambda x: [x[match] for match in match_on]

result = []
for (source, dest, service, log, virt), group in groupby(input_, groupfunc):
    group_ = tuple(group)  # otherwise iterator goes bye-bye
    res = {'Destination IP': ', '.join(d['Destination IP'] for d in group_),
           'Services': service,
           'Source IP': ', '.join(d['Source IP'] for d in group_),
           's_destination_zone': dest,
           's_logical_system': log,
           's_matched_route': ', '.join(d['s_matched_route'] for d in group_),
           's_source_zone': source,
           's_virtual_router': virt}
    result.append(res)

从您的示例中推断一下,看起来实际上有5个要匹配的键/值对。 (否则,你会以某种方式组合重复/相同的值,而你没有。)

此处的lambda func匹配输入中的字典,基于它们具有match_on中5个键的等效值的交集。所有匹配的字典都被抛入group,这里是一个itertools._grouper迭代器。