Skip to content

Instantly share code, notes, and snippets.

@clvrobj
Forked from liluo/gist:1957856
Created March 2, 2012 11:17
Show Gist options
  • Save clvrobj/1957858 to your computer and use it in GitHub Desktop.
Save clvrobj/1957858 to your computer and use it in GitHub Desktop.
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)
@clvrobj
Copy link
Author

clvrobj commented Mar 2, 2012

Any Python version, haha. @liluo
Refer to https://gist.github.com/1958259.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment