Inflate a flattened dictionary
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 inflate(d, sep="_"): | |
items = dict() | |
for k, v in d.items(): | |
keys = k.split(sep) | |
sub_items = items | |
for ki in keys[:-1]: | |
try: | |
sub_items = sub_items[ki] | |
except KeyError: | |
sub_items[ki] = dict() | |
sub_items = sub_items[ki] | |
sub_items[keys[-1]] = v | |
return items |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment