Created
May 29, 2019 15:48
-
-
Save nicksnell/284a4a6ba8ff64e778d7ff62746e6344 to your computer and use it in GitHub Desktop.
Recursively work out deltas of numbers in dictionaries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def delta_dict(a, b): | |
result = {} | |
for key in a.keys(): | |
if isinstance(a[key], dict): | |
result[key] = delta_dict(a[key], b.get(key, {})) | |
else: | |
result[key] = a[key] - b.get(key, 0) | |
return result | |
if __name__ == '__main__': | |
print(delta_dict({'a': {'c': 4, 'd': 2}, 'b': 2}, {'a': {'c': 8}, 'b': 4})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment