Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- d1 = {"a1": {"b11": "0", "b12": "1"}, "a2": {"b21": "2", "b22": "3"}, "a3": {"b32": "3"}}
- d2 = {"a1": {"b11": "4", "b12": "1"}, "a2": {"b21": "2", "b22": "3"}, "a3": {"b32": "3"}, "a4": {"b41": "5"}}
- def compare_dictionaries(dict_1, dict_2, dict_1_name, dict_2_name, path=""):
- err = ''
- key_err = ''
- value_err = ''
- old_path = path
- for k in dict_1.keys():
- path = old_path + "[%s]" % k
- if not dict_2.has_key(k):
- key_err += "Key %s%s not in %sn" % (dict_2_name, path, dict_2_name)
- else:
- if isinstance(dict_1[k], dict) and isinstance(dict_2[k], dict):
- err += compare_dictionaries(dict_1[k],dict_2[k],'d1','d2', path)
- else:
- if dict_1[k] != dict_2[k]:
- value_err += "Value of %s%s (%s) not same as %s%s (%s)n"
- % (dict_1_name, path, dict_1[k], dict_2_name, path, dict_2[k])
- for k in dict_2.keys():
- path = old_path + "[%s]" % k
- if not dict_1.has_key(k):
- key_err += "Key %s%s not in %sn" % (dict_2_name, path, dict_1_name)
- return key_err + value_err + err
- print compare_dictionaries(d1, d2, 'dict1', 'dict2')
- dict2[a4] not in dict1
- Value of dict1[a1][b11] (0) not same as dict2[a1][b11] (4)
- Value of dict1[a2][b22] (3) not same as dict2[a2][b22] (6)
- ................................
- | d1 | d2 |
- --------------------------------
- | Changed: |
- | [a1][b11]=0 | [a1][b11]=4 |
- | [a2][b22]=3 | [a2][b22]=6 |
- | Unique: |
- | | [a4] |
- ````````````````````````````````
Add Comment
Please, Sign In to add comment