【发布时间】:2017-01-27 14:53:13
【问题描述】:
我有两个字典列表代表两个表的行,所以:
tableA = [{"id": 1, "name": "foo"}, {"id": 2, "name": "bar"}]
tableB = [{"id": 1, "name": "bar"}, {"id": 3, "name": "baz"}]
我想通过以下方式获取差异:
added = [{"id": 3, "name": "baz"}]
updated = [{"id": 1, "name": "bar"}]
我知道,id 是独一无二的。
所以,我打算循环遍历tableB,询问id,如果它们相等,然后比较字典以知道它是否是updated,如果不是,则id 是新的,它是@987654327 @。
for x in tableA:
idexists = false
for y in tableY:
if x["id"] == y["id"]:
if x != y:
updated.append(x)
idexists = true
if not idexists:
added.append(x)
正确吗?可以用pythonic的方式完成吗?
【问题讨论】:
-
我不清楚
added和updated是如何计算的。
标签: python algorithm list dictionary set