Created
April 4, 2016 03:22
-
-
Save iamsk/92bed0bc3a369e4b4ad131347e2f2327 to your computer and use it in GitHub Desktop.
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
from collections import defaultdict | |
def defaultdict2dict(d): | |
_d = {} | |
for k, v in d.items(): | |
if isinstance(v, defaultdict): | |
_d[k] = defaultdict2dict(v) | |
else: | |
_d[k] = v | |
return _d | |
if __name__ == "__main__": | |
temp = defaultdict(lambda: defaultdict(lambda: defaultdict())) | |
temp[1][2][3] = 1 | |
temp[2][3] = 1 | |
temp[3] = 1 | |
assert isinstance(temp, defaultdict) | |
assert isinstance(defaultdict2dict(temp), dict) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment