之前写 Python 时,要比较两个字典的差异,自己写的比较 Low,然后在 github 上发现一个不错的类库,拿来就用了。
github: https://github.com/hughdbrown/dictdiffer
我的代码示例:
class DictDiffer(object):
"""
Calculate the difference between two dictionaries as:
(1) items added
(2) items removed
(3) keys same in both but changed values
(4) keys same in both and unchanged values
"""
def __init__(self, current_dict, past_dict):
self.current_dict, self.past_dict = current_dict, past_dict
self.current_keys, self.past_keys = set(current_dict.keys()), set(past_dict.keys())
self.intersect = self.current_keys.intersection(self.past_keys)
def added(self):
""" Find keys that have been added """
return self.current_keys - self.intersect
def removed(self):
""" Find keys that have been removed """
return self.past_keys - self.intersect
def changed(self):
""" Find keys that have been changed """
return set(o for o in self.intersect
if self.past_dict[o] != self.current_dict[o])
def unchanged(self):
""" Find keys that are unchanged """
return set(o for o in self.intersect
if self.past_dict[o] == self.current_dict[o])
def new_or_changed(self):
""" Find keys that are new or changed """
# return set(k for k, v in self.current_dict.items()
# if k not in self.past_keys or v != self.past_dict[k])
return self.added().union(self.changed())
var1 = {'a': 1, 'b': 1, 'c': 0}
var2 = {'a': 1, 'b': 2, 'd': 3, 'e': 4}
c = DictDiffer(var2, var1)
print('增加', c.added())
print('减少', c.removed())
print('变更', c.changed())
# 测试
# python dictdiff.py
#增加 {'e', 'd'}
#减少 {'c'}
#变更 {'b'}