Skip to content

Instantly share code, notes, and snippets.

@xiaoluoboding
Last active October 22, 2015 02:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiaoluoboding/8a90d65c4a2cc51272f0 to your computer and use it in GitHub Desktop.
Save xiaoluoboding/8a90d65c4a2cc51272f0 to your computer and use it in GitHub Desktop.
Python Code Snippets
#把一个逗号分隔的词典,转成嵌套多层的词典。如:
dotted_config = {'a.b.c': 'd'}
nest_config = convert(dotted_config) // {'a': {'b': {'c': 'd'}}}
# 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