Created
July 23, 2016 19:49
-
-
Save mhoff/53be2c14195dbeb424722009da5d730b 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
def dmerge(x, (k,v)): | |
if k in x: | |
dmerge(x[k], v.items()[0]) | |
else: | |
x[k] = v | |
return x | |
def hdict_from_dict(src): | |
return reduce(lambda x, y: dmerge(x, y), [hdict(k, v).items()[0] for k, v in src.items()], {}) | |
def hdict(keys, value, sep="/"): | |
return reduce(lambda v, k: {k: v}, reversed(keys.split(sep)), value) | |
data = { | |
"a/b/c": 10, | |
"a/b/d": 20, | |
"a/e": "foo", | |
"a/f": False, | |
"g": 30 } | |
print("flat:", data) | |
print("tree:", hdict_from_dict(data)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment