-
-
Save clvrobj/1957858 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
r1 = {'a': {'q': 1, 'w': 2}, 'b': {'q': 3, 'w': 4}} | |
r2 = {'a': {'q': 5, 'w': 7}, 'b': {'q': 8, 'w': 2}} | |
# ret = {'a': {'q': 6, 'w': 9}, 'b': {'q': 11, 'w': 6}} | |
def iter_dict(x, y): | |
if not isinstance(x, dict): | |
return map(lambda x: sum(x), zip(x, y)) | |
if isinstance(x.values()[0], dict): | |
return dict(zip(x.keys(), map(lambda x:iter_dict(x[0], x[1]), zip(x.values(), y.values())))) | |
return dict(zip(x.keys(), iter_dict(x.values(), y.values()))) | |
print iter_dict(r1, r2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Any Python version, haha. @liluo
Refer to https://gist.github.com/1958259.