Created
August 19, 2014 05:16
-
-
Save chansuke/9737cdca87d6cff9691d to your computer and use it in GitHub Desktop.
Check iO - The Flat 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 flatten(dictionary): | |
stack = [((), dictionary)] | |
result = {} | |
while stack: | |
path, current = stack.pop() | |
for k, v in current.items(): | |
if isinstance(v, dict): | |
stack.append((path + (k,), v)) | |
else: | |
result["/".join((path + (k,)))] = v | |
if len(current) == 0: | |
result["/".join(path)] = "" | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment