python - 匹配两个字典列表中的值

标签 python dictionary nested cross-reference

船员,

我从我的两个交换机中提取了以下两个词典列表。第一个来 self 们的核心交换机,第二个来自访问“switch1”。为了更深入地了解这个网络,我想交叉引用这两个词典列表。

core_table = [
{'ip': '172.17.2.1', 'mac': 'b8:27:eb:a3:24:a0', 'host': 'laptop', 'if': 'switch1'}, 
{'ip': '172.17.6.3', 'mac': '0c:89:10:88:20:3f', 'host': 'desktop', 'if': 'switch3'},
{'ip': '172.17.7.2', 'mac': 'b8:27:eb:a3:24:a3', 'host': 'server', 'if': 'switch1'}, 
{'ip': '172.17.6.2', 'mac': '0c:89:10:88:20:3f', 'host': 'storage', 'if': 'switch2'}
]

switch1_table = [
{'mac': '00:01:02:71:44:59', 'if': 'ge-1/0/6.0'},
{'mac': '00:06:73:00:c9:7a', 'if': 'ge-3/0/2.0'},
{'mac': 'b8:27:eb:a3:24:a0', 'if': 'ge-5/0/27.0'}, 
{'mac': '00:09:0f:09:1d:06', 'if': 'ae0.0'},
{'mac': '00:0a:07:05:1f:a4', 'if': 'ge-2/0/15.0'}, 
{'mac': '00:0b:4f:5d:09:ae', 'if': 'ge-2/0/19.0'},
{'mac': '00:0b:4f:d3:7b:3f', 'if': 'ge-1/0/18.0'}, 
{'mac': '00:0c:29:49:64:12', 'if': 'ae0.0'},
{'mac': '00:0e:ec:e8:18:50', 'if': 'ae0.0'}, 
{'mac': 'b8:27:eb:a3:24:a3', 'if': 'ge-0/0/44.0'}, 
{'mac': '00:15:17:93:aa:01', 'if': 'ae0.0'}
]

我可以总结 core_table 中的所有 mac 地址:

for x in [m['mac'] for m in core_table]:
     print x
     print m

如果我打印 m,我只会看到列表中的最后一个字典。

同样,access_table 中的所有接口(interface):

for y in [n['if'] for n in ex_table]:
      print y
      print n

好像这篇文章:Python - accessing values nested within dictionaries接近我需要的,但这依赖于一个字典列表中条目之间的关系。不是这种情况。

在将 MAC 地址与 switch1_table 匹配时,我找不到迭代 core_table 的方法。如果 core_table 中的 mac 地址与 swicth1_table 中的 mac 地址匹配,我想打印 core_table 的相应行和 access_table 的“if”值。我想要实现的目标:

{'ip': '172.17.2.1', 'mac': 'b8:27:eb:a3:24:a0', 'host': 'laptop', 'if': 'switch1' 'port': 'ge-5/0/27.0'},
{'ip': '172.17.7.2', 'mac': 'b8:27:eb:a3:24:a3', 'host': 'server', 'if': 'switch1' 'port': 'ge-0/0/44.0'}

我想出了:(谢谢:Pythonic Way to Compare Values in Two Lists of Dictionaries)

match = [x['mac'] for x in core_table if 'switch1' in x['if']]
print [x for x in switch1_table if x['mac'] in match]

这让我很接近,但现在我找不到添加主机和 IP 数据的方法。

欢迎任何建议。

最佳答案

for x in [m['mac'] for m in core_table]:
     print x
     print m

您不应尝试从外部访问m。请参阅this .

创建列表推导式是为了简化事情并使代码更清晰。如果您在许多嵌套级别中使用它们,您就违背了它们的目的。 对于 Python 2.7.x

from copy import copy

result = []
for item in core_table:
    for item2 in swicth1_table:
        if item['mac'] == item2['mac']:
            new_item = copy(item)
            new_item['port'] = item2['if']
            result.append(new_item)

输出

{'port': 'ge-5/0/27.0', 'ip': '172.17.2.1', 'mac': 'b8:27:eb:a3:24:a0', 'host': 'laptop', 'if': 'switch1'}
{'port': 'ge-0/0/44.0', 'ip': '172.17.7.2', 'mac': 'b8:27:eb:a3:24:a3', 'host': 'server', 'if': 'switch1'}

关于python - 匹配两个字典列表中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41165295/

相关文章:

python在嵌套列表中找到最大的自由方 block

python - 获取 pandas 中分类变量的映射

python - 从列表中删除已存在的元素并仅在列表中保留该值的一个元素

python - 使用 Numpy 高效求和复杂矩阵乘积

python - 嵌套列表到字典

python - 如何在 Python 中搜索嵌套列表网格并给出字母坐标?

python - Stata加载共享库python39.dll失败

python - 如何在Python中的另一个循环中正确编写一个for循环?

python - 为什么python字典占用这么多内存?

PHP如何循环遍历多个注释级别