【发布时间】:2021-01-18 23:14:21
【问题描述】:
正如标题所说,我有两个字典列表。我们将它们称为 A,它存在于数据库中,而 B,它是来自传感器的实时结果。
A 与 B 共享键/值
示例如下:
A = [
{
"created_at": "2020-09-19T17:25:29.547354",
"id": 1,
"ip_address": "192.168.1.1",
"mac_address": "xx:xx:xx:xx:xx:xx",
},
{
"created_at": "2020-09-19T17:25:29.564472",
"id": 2,
"ip_address": "192.168.1.2",
"mac_address": "xx:xx:xx:xx:xx:xx",
},
{
"created_at": "2020-09-19T17:25:29.564472",
"id": 3,
"ip_address": "192.168.1.3",
"mac_address": "xx:xx:xx:xx:xx:xx",
}
]
B = [
{
'mac_address': 'xx:xx:xx:xx:xx:xx',
'ip_address': '192.168.1.1',
'status': True
},
{
'mac_address': 'xx:xx:xx:xx:xx:xx',
'ip_address': '192.168.1.2',
'status': True
}
]
根据值ip_address,找出 B 和 A 中缺失字典的最佳方法是什么。
例如,我们可以通过查看上面的内容来判断,包含 ip_address “192.168.1.3” 的字典在 B 中不存在。目的是尝试找到不存在于 B 之间的值列表两个,如果有的话。
感谢任何帮助!
编辑:
预期的输出是一个类似的列表:["192.168.1.3"]
【问题讨论】:
-
需要匹配什么?只是IP地址还是mac地址?你想要的输出是什么? IP 地址,在
A中找到的字典,还有什么? -
好主意,我会更新帖子以显示预期输出
-
Syn,我认为 CHRIS 方法的有效方法是制作两组并找到差异。看看我对你代码的 1000000 次迭代的回答
标签: python arrays list dictionary multidimensional-array