Difference between tables
我有两个字典列表,代表两个表的行,因此:
1 2 3 | tableA = [{"id": 1,"name":"foo"}, {"id": 2,"name":"bar"}] tableB = [{"id": 1,"name":"bar"}, {"id": 3,"name":"baz"}] |
我想通过以下方式获得差异:
1 2 3 | added = [{"id": 3,"name":"baz"}] updated = [{"id": 1,"name":"bar"}] |
号
我知道,
因此,我计划循环访问
1 2 3 4 5 6 7 8 9 | 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) |
对吗?可以用Python的方式来做吗?
我将把这些表重组成更为方便的
1 2 3 4 5 6 7 8 9 10 11 | from deepdiff import DeepDiff tableA = [{"id": 1,"name":"foo"}, {"id": 2,"name":"bar"}] tableB = [{"id": 1,"name":"bar"}, {"id": 3,"name":"baz"}] tableA = {item["id"]: item["name"] for item in tableA} tableB = {item["id"]: item["name"] for item in tableB} print(DeepDiff(tableA, tableB)) |
印刷品:
1 2 3 4 5 6 7 8 9 10 | { 'dictionary_item_added': {'root[3]'}, 'dictionary_item_removed': {'root[2]'}, 'values_changed': { 'root[1]': { 'old_value': 'foo', 'new_value': 'bar' } } } |
号
为了计算这里建议使用的
另一个想法可能是将字典列表转换为
- 并排输出两个熊猫数据帧中的差异-突出差异
- 计算数据帧组内的差异