Python-有条件地加入两个字典列表

时间:2019-04-10 16:16:17

标签: python json dictionary

我有以下两个字典列表,如果逻辑上匹配(如果remote_addr在子网内),我想加入那些字典项

我具有如下逻辑来测试主机是否在子网内,如下所示,其中n是字典1,dcs是字典2,但是我不确定如何循环这两个列表以获取联接? / p>

if ipaddress.ip_address(n['remote_addr']) in ipaddress.ip_interface('{}{}'.format(dcs['subnet'],dcs['subnet_mask'])).network:
    # join the two matches


device_circuits = [
    {
        "hostname": "EDGE",
        "circuit_name": "MPLS 01",
        "circuit_preference": "Primary",
        "circuit_id": 456,
        "subnet": "1.1.1.1",
        "subnet_mask": "/30",
        "subnet_type": "MPLS"
    },
    {
        "hostname": "EDGE",
        "circuit_name": "MPLS 02",
        "circuit_preference": "Secondary",
        "circuit_id": 123,
        "subnet": "1.1.1.5",
        "subnet_mask": "/30",
        "subnet_type": "MPLS"
    }
    {
        "hostname": "EDGE",
        "circuit_name": "DSL",
        "circuit_preference": "Tertiary",
        "circuit_id": 999,
        "subnet": "3.3.3.3",
        "subnet_mask": "/30",
        "subnet_type": "DSL"
    }    
]

bgp_sum = [
    {
        "remote_addr ": "1.1.1.2 ",
        "remote_as ": "1",
        "uptime ": "never ",
        "accepted_prefixes ": "Active "
    },
    {
        "remote_addr ": "1.1.1.6 ",
        "remote_as ": "2",
        "uptime ": "3w5d ",
        "accepted_prefixes ": "217 "
    },
    {
        "remote_addr ": "2.2.2.2 ",
        "remote_as ": "10",
        "uptime ": "3w5d ",
        "accepted_prefixes ": "217 "
    },    
]

下面的目标数据,所有未找到的bgp_sum都将被丢弃,并且device_circuits将根据上述逻辑合并到记录中

data = [
    {
        "hostname": "EDGE",
        "circuit_name": "MPLS 01",
        "circuit_preference": "Primary",
        "circuit_id": 456,
        "subnet": "1.1.1.1",
        "subnet_mask": "/30",
        "subnet_type": "MPLS",
        "remote_addr ": "1.1.1.2 ",
        "remote_as ": "1",
        "uptime ": "never ",
        "accepted_prefixes ": "Active "
    },
    {
        "hostname": "EDGE",
        "circuit_name": "MPLS 02",
        "circuit_preference": "Secondary",
        "circuit_id": 123,
        "subnet": "1.1.1.5",
        "subnet_mask": "/30",
        "subnet_type": "MPLS",
        "remote_addr ": "1.1.1.6 ",
        "remote_as ": "2",
        "uptime ": "3w5d ",
        "accepted_prefixes ": "217 "        
    }
    {
        "hostname": "EDGE",
        "circuit_name": "DSL",
        "circuit_preference": "Tertiary",
        "circuit_id": 999,
        "subnet": "3.3.3.3",
        "subnet_mask": "/30",
        "subnet_type": "DSL"
    }    
]

1 个答案:

答案 0 :(得分:1)

我无法理解合并两个字典的逻辑,但是由于您已经提到了,您具有有效的逻辑,所以我将在循环部分为您提供帮助。您需要有一个嵌套循环,然后.update()来附加值。

arr_1 = [
    {
        'a': 12,
        'b': 23
    },
    {
        'a': 121,
        'b': 231
    },
    {
        'a': 122,
        'b': 232
    }
]


arr_2 = [
    {
        'a': 12,
        'c': 77
    },
    {
        'a': 121,
        'c': 88
    },
    {
        'a': 122,
        'c': 99
    }
]

for elem_1 in arr_1:
    for elem_2 in arr_2:
        if elem_1['a'] == elem_2['a']: // update your match logic here
            elem_1.update(elem_2)
print(arr_1)