这是我的代码:
from collections import defaultdict
ip_port_device = [
{'ip': '192.168.1.140', 'port_number': 4, 'device_name': 'device1'},
{'ip': '192.168.1.128', 'port_number': 8, 'device_name': 'device1'},
{'ip': '192.168.1.56', 'port_number': 14, 'device_name': 'device1'},
{'ip': '192.168.1.61', 'port_number': 4, 'device_name': 'device1'},
{'ip': '192.168.1.78', 'port_number': 8, 'device_name': 'device1'},
{'ip': '192.168.1.13', 'port_number': 16, 'device_name': 'device1'},
{'ip': '192.168.2.140', 'port_number': 4, 'device_name': 'device2'},
{'ip': '192.168.2.128', 'port_number': 8, 'device_name': 'device2'},
{'ip': '192.168.2.56', 'port_number': 14, 'device_name': 'device2'},
{'ip': '192.168.2.61', 'port_number': 4, 'device_name': 'device2'},
{'ip': '192.168.2.78', 'port_number': 8, 'device_name': 'device2'},
{'ip': '192.168.2.13', 'port_number': 16, 'device_name': 'device2'},
{'ip': '192.168.3.140', 'port_number': 4, 'device_name': 'device3'},
{'ip': '192.168.3.128', 'port_number': 8, 'device_name': 'device3'},
{'ip': '192.168.3.56', 'port_number': 14, 'device_name': 'device3'},
{'ip': '192.168.3.61', 'port_number': 4, 'device_name': 'device3'},
{'ip': '192.168.3.78', 'port_number': 8, 'device_name': 'device3'},
{'ip': '192.168.3.13', 'port_number': 16, 'device_name': 'device3'},
]
ip_per_node = [
{'node_name': 'server9.example.com', 'ip_address': '192.168.1.140'},
{'node_name': 'server19.example.com', 'ip_address': '192.168.1.128'},
{'node_name': 'server11.example.com', 'ip_address': '192.168.2.140'},
{'node_name': 'server21.example.com', 'ip_address': '192.168.2.128'},
{'node_name': 'server17.example.com', 'ip_address': '192.168.3.140'},
{'node_name': 'server6.example.com', 'ip_address': '192.168.3.128'},
]
ips_and_ports_in_switch = []
for compute in ip_per_node:
for port in ip_port_device:
if compute['ip_address'] == port['ip']:
port = port['port_number']
for new_port in ip_port_device:
if port == new_port['port_number']:
ips_and_ports_in_switch.append({
'port_number': new_port['port_number'],
'ip_address': new_port['ip'],
'node_name': compute['node_name'],
'device_name': new_port['device_name']
})
concatenated = defaultdict(list)
for entry in ips_and_ports_in_switch:
concatenated[(entry['device_name'], entry['port_number'], entry['node_name'])].append(entry['ip_address'])
逻辑是:
如果ip_per_node['ip_address']
匹配ip_port_device['ip']
,则ip_port_device
查找所有 ip 具有相同的端口号。
然后像这样保存(预期输出):
node server9.example.com, port 4, device device1, ips ['192.168.1.140', '192.168.1.61']
node server19.example.com, port 8, device device1, ips ['192.168.1.128', '192.168.1.78']
node server11.example.com, port 4, device device2, ips ['192.168.2.140', '192.168.2.61']
node server21.example.com, port 8, device device2, ips ['192.168.2.128', '192.168.2.78']
node server17.example.com, port 4, device device3, ips ['192.168.3.140', '192.168.3.61']
node server6.example.com, port 8, device device3, ips ['192.168.3.128', '192.168.3.78']
我当前的代码无法按我的预期工作。它为所有节点多次保存一个端口。
我尝试为样本添加最少但需要的数据。
使其产生预期结果所需的修改如下(其中带有 Booboo 的注释是对代码所做的主要更改)
印刷: