- Ниже приведен текущий подход, но он работает медленно на больших наборах данных, я ищу способ сделать его более эффективным и быстрым.
def DictChecker(old_dict,new_dict):
final_added = {}
final_removed = {}
for k,v in new_dict.items():
if k not in old_dict.keys():
old_dict[k] = v
final_added[k] = v
for k,v in old_dict.items():
if old_dict.keys() != new_dict.keys():
if k not in new_dict.keys():
final_removed[k] = v
for i,j in new_dict.items():
old_dict_values = old_dict.get(i)
if (j == old_dict_values):
break
else:
added = list(sorted(set(j) - set(old_dict_values)))
removed = list(sorted(set(old_dict_values) - set(j)))
final_added[i] = added
final_removed[i] = removed
return final_added,final_removed
current = {"CNAME":[1,2,3,4],"MX":[2,3,1],"WOOT":[1,2,3,4]}
new = {"CNAME":[5,2,10],"MX":[1,4],"AAA":[100,200]}
added,removed = DictChecker(current,new)
print(added)
>> {'AAA': [100, 200], 'CNAME': [5, 10], 'MX': [4]}
print(removed)
>> {'WOOT': [1, 2, 3, 4], 'CNAME': [1, 3, 4], 'MX': [2, 3]}