需要帮助以根据词典从词典中创建组

时间:2019-07-15 06:51:57

标签: python

我正在尝试根据索引值匹配来创建单个组。一旦值不匹配,应创建一个新组。

这是我到目前为止尝试过的。我已经按照acl_name_list中的名称对列表进行了分组。现在,我想加一个更深的级别,以便获得预期的结果。

acl_name_list = ['SPLIT-ACL', 'outside_cryptomap_HPS-CHI', 'outside_cryptomap_1', 'inside_access_in', 'dsi_split_tunnel']

for i in data:
  order_list_key.append({k : i[k] for k in key_order})

output_dict = collections.defaultdict(list)
for data_dict in order_list_key:
    line_txt = ' '.join(list(filter(None, data_dict.values())))
    output_key = []
    for k, v in data_dict.items():
        if k in acl_name_list:
            output_key.append(v)
    output_key = ' '.join(output_key)
    output_dict[' ' .join(list(filter(None,data_dict.values()))[0:1])].append(line_txt.strip())

output = json.dumps(output_dict, indent=4, separators=(',',' : '))
print(output)

预期结果:

{
    "SPLIT-ACL" : [
        "SPLIT-ACL standard permit 10.20.0.0 255.255.255.0",
        "SPLIT-ACL standard permit 10.21.0.0 255.255.255.0"
        ],
    "SPLIT-ACL" : [
        "SPLIT-ACL standard deny 10.22.0.0 255.255.255.0"
        ],
    "SPLIT-ACL" : [
        "SPLIT-ACL standard permit 10.23.0.0 255.255.255.0",
        "SPLIT-ACL standard permit 10.24.0.0 255.255.255.0",
        "SPLIT-ACL standard permit 10.25.0.0 255.255.255.0",
        "SPLIT-ACL standard permit 10.26.0.0 255.255.255.0"
    ],
    "SPLIT-ACL" : [
        "SPLIT-ACL remark Warren, NJ",
        "SPLIT-ACL remark Chicago",
        "SPLIT-ACL remark Chicago"
    ],      
    "outside_cryptomap_HPS-CHI" : [
        "outside_cryptomap_HPS-CHI extended permit object service-ftp-dst object KOP-LAN object CHI-LAN log",
        "outside_cryptomap_HPS-CHI extended permit object service-ftp-dst object HPS-NJ-LAN object CHI-LAN log",
        "outside_cryptomap_HPS-CHI extended permit object service-ftp-dst object KOP-RA-VPN object CHI-LAN log"
    ],
    "outside_cryptomap_1" : [
        "outside_cryptomap_1 extended permit ip object HPS-NJ-LAN object CHI-LAN",
        "outside_cryptomap_1 extended permit ip object KOP-RA-VPN object CHI-LAN"
    ],
    "inside_access_in" : [
        "inside_access_in extended permit object service-ftp-src object SQL_Forest object actavis_hci_lan inactive",
        "inside_access_in extended permit object service-ftp-src host 10.20.10.92 object actavis_hci_lan inactive",
        "inside_access_in extended permit object service-ftp-src host 10.20.10.93 object actavis_hci_lan inactive"
        ],
    "inside_access_in" : [
        "inside_access_in extended deny object service-ftp-src host 10.20.10.94 object actavis_hci_lan inactive"
        ],
    "inside_access_in" : [
        "inside_access_in extended permit object service-ftp-src host 10.20.10.96 object actavis_hci_lan inactive",
        "inside_access_in extended permit object service-ftp-src host 10.20.10.97 object actavis_hci_lan inactive"
        ],
    "inside_access_in" : [
        "inside_access_in extended permit tcp host 10.20.10.246 host 10.100.208.21 gt 8080 log emergencies interval 100 time-range WEEKDAYS inactive",
        "inside_access_in extended permit tcp host 10.20.10.247 host 10.100.208.21 gt 8080 log emergencies interval 100 time-range WEEKDAYS inactive",
        "inside_access_in extended permit tcp host 10.20.10.250 host 10.100.208.22 gt 8080 log emergencies interval 100 time-range WEEKDAYS inactive",
        "inside_access_in extended permit tcp host 10.20.10.251 host 10.100.208.23 gt 8080 log emergencies interval 100 time-range WEEKDAYS inactive"
        ],
    "inside_access_in" : [
        "inside_access_in extended deny udp object-group HTTP_1 object mgmtzone_servers lt 23 log"
        ],
    "inside_access_in" : [
        "inside_access_in extended deny ip any4 object ISCSI_Prod",
        "inside_access_in extended deny ip any4 object actavis_hci_lan"
        ],
    "inside_access_in" : [
        "inside_access_in extended permit ip any4 172.20.10.0 255.255.255.0"
        ],
    "inside_access_in" : [
        "inside_access_in extended deny ip any4 172.20.11.0 255.255.255.0",
        "inside_access_in extended deny ip any4 172.20.12.0 255.255.255.0",
        "inside_access_in extended deny ip any4 172.20.13.0 255.255.255.0"
    ],
    "dsi_split_tunnel" : [
        "dsi_split_tunnel standard permit 172.16.24.0 255.255.255.0",
        "dsi_split_tunnel standard permit 172.16.25.0 255.255.255.0"
        ],
    "dsi_split_tunnel" : [
        "dsi_split_tunnel standard deny 172.25.100.0 255.255.255.0"
        ],
    "dsi_split_tunnel" : [
        "dsi_split_tunnel standard permit 172.25.82.0 255.255.255.0"
        ],
    "dsi_split_tunnel" : [
        "dsi_split_tunnel standard deny 172.25.83.0 255.255.255.0"
        ],
    "dsi_split_tunnel" : [
        "dsi_split_tunnel standard permit 172.25.85.0 255.255.255.0"
        ],
    "dsi_split_tunnel" : [
        "dsi_split_tunnel standard deny 172.25.86.0 255.255.255.0",
        "dsi_split_tunnel standard deny 172.25.87.0 255.255.255.0"
        ],
    "dsi_split_tunnel" : [
        "dsi_split_tunnel standard permit 192.168.10.0 255.255.255.0",
        "dsi_split_tunnel standard permit 192.168.11.0 255.255.255.0"
    ]
}

实际结果:

{
    "SPLIT-ACL" : [
        "SPLIT-ACL standard permit 10.20.0.0 255.255.255.0",
        "SPLIT-ACL remark Warren, NJ",
        "SPLIT-ACL standard permit 10.21.0.0 255.255.255.0",
        "SPLIT-ACL standard deny 10.22.0.0 255.255.255.0",
        "SPLIT-ACL remark Chicago",
        "SPLIT-ACL standard permit 10.23.0.0 255.255.255.0",
        "SPLIT-ACL standard permit 10.24.0.0 255.255.255.0",
        "SPLIT-ACL standard permit 10.25.0.0 255.255.255.0",
        "SPLIT-ACL remark Chicago",
        "SPLIT-ACL standard permit 10.26.0.0 255.255.255.0"
    ],
    "outside_cryptomap_HPS-CHI" : [
        "outside_cryptomap_HPS-CHI extended permit object service-ftp-dst object KOP-LAN object CHI-LAN log",
        "outside_cryptomap_HPS-CHI extended permit object service-ftp-dst object HPS-NJ-LAN object CHI-LAN log",
        "outside_cryptomap_HPS-CHI extended permit object service-ftp-dst object KOP-RA-VPN object CHI-LAN log"
    ],
    "outside_cryptomap_1" : [
        "outside_cryptomap_1 extended permit ip object HPS-NJ-LAN object CHI-LAN",
        "outside_cryptomap_1 extended permit ip object KOP-RA-VPN object CHI-LAN"
    ],
    "inside_access_in" : [
        "inside_access_in extended permit object service-ftp-src object SQL_Forest object actavis_hci_lan inactive",
        "inside_access_in extended permit object service-ftp-src host 10.20.10.92 object actavis_hci_lan inactive",
        "inside_access_in extended permit object service-ftp-src host 10.20.10.93 object actavis_hci_lan inactive",
        "inside_access_in extended deny object service-ftp-src host 10.20.10.94 object actavis_hci_lan inactive",
        "inside_access_in extended permit object service-ftp-src host 10.20.10.96 object actavis_hci_lan inactive",
        "inside_access_in extended permit object service-ftp-src host 10.20.10.97 object actavis_hci_lan inactive",
        "inside_access_in extended permit tcp host 10.20.10.246 host 10.100.208.21 gt 8080 log emergencies interval 100 time-range WEEKDAYS inactive",
        "inside_access_in extended permit tcp host 10.20.10.247 host 10.100.208.21 gt 8080 log emergencies interval 100 time-range WEEKDAYS inactive",
        "inside_access_in extended permit tcp host 10.20.10.250 host 10.100.208.22 gt 8080 log emergencies interval 100 time-range WEEKDAYS inactive",
        "inside_access_in extended permit tcp host 10.20.10.251 host 10.100.208.23 gt 8080 log emergencies interval 100 time-range WEEKDAYS inactive",
        "inside_access_in extended deny udp object-group HTTP_1 object mgmtzone_servers lt 23 log",
        "inside_access_in extended deny ip any4 object ISCSI_Prod",
        "inside_access_in extended deny ip any4 object actavis_hci_lan",
        "inside_access_in extended permit ip any4 172.20.10.0 255.255.255.0",
        "inside_access_in extended deny ip any4 172.20.11.0 255.255.255.0",
        "inside_access_in extended deny ip any4 172.20.12.0 255.255.255.0",
        "inside_access_in extended deny ip any4 172.20.13.0 255.255.255.0"
    ],
    "dsi_split_tunnel" : [
        "dsi_split_tunnel standard permit 172.16.24.0 255.255.255.0",
        "dsi_split_tunnel standard permit 172.16.25.0 255.255.255.0",
        "dsi_split_tunnel standard deny 172.25.100.0 255.255.255.0",
        "dsi_split_tunnel standard permit 172.25.82.0 255.255.255.0",
        "dsi_split_tunnel standard deny 172.25.83.0 255.255.255.0",
        "dsi_split_tunnel standard permit 172.25.85.0 255.255.255.0",
        "dsi_split_tunnel standard deny 172.25.86.0 255.255.255.0",
        "dsi_split_tunnel standard deny 172.25.87.0 255.255.255.0",
        "dsi_split_tunnel standard permit 192.168.10.0 255.255.255.0",
        "dsi_split_tunnel standard permit 192.168.11.0 255.255.255.0"
    ]
}

0 个答案:

没有答案