Last active
October 22, 2015 02:40
-
-
Save xiaoluoboding/8a90d65c4a2cc51272f0 to your computer and use it in GitHub Desktop.
Python Code Snippets
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
#把一个逗号分隔的词典,转成嵌套多层的词典。如: | |
dotted_config = {'a.b.c': 'd'} | |
nest_config = convert(dotted_config) // {'a': {'b': {'c': 'd'}}} |
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
# Python 3 | |
def convert(dotted_config): | |
nest_config = {} | |
for key, value in dotted_config.items(): | |
parts = key.split('.') | |
parent = nest_config | |
key = parts.pop(0) | |
while parts: | |
parent = parent.setdefault(key, {}) | |
key = parts.pop(0) | |
parent[key] = value | |
return nest_config |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment