字典中的差异 [英] Difference in a dict

查看:58
本文介绍了字典中的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚字典中的差异,无论是添加还是删除内容以及内容.

I'm trying to figure out the difference in a dict, whether something was added or removed and from what.

在这种情况下,会添加一个值:

Here is a case where a value is added:

original = {0: None, 1: False, 2: [16]}
new = {0: None, 1: False, 2: [2, 16]}

difference = True, {2: 2} # True = Added

这是一个值被删除的情况:

And here is a case where a value is removed:

original = {0: None, 1: False, 2: [16, 64]}
new = {0: None, 1: False, 2: [64]}

difference = False, {2: 16} # False = Removed

问题是我不知道该如何区别.会有人碰巧知道如何实现这样的结果吗?

The problem is that I have no idea how to recieve the difference. Would anyone happen to know how to achieve such a result?

其他信息(不知道是否需要此信息):

Extra information (no idea if you'll need this):

  • 这也可以应用于原始和新的0和1.
  • 1和2不能同时处于活动状态.如果一个具有值,则另一个为False.

推荐答案

正如我在

As I've explained in an other question there is a library on PyPI just for this task, which is datadiff library. It's easy to use and you can use the output to do what you have to do.

这篇关于字典中的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆