python - Convert a list into a nested dictionary -
python - Convert a list into a nested dictionary -
for illustration have
x = ['a','b','c']
i need convert to:
y['a']['b']['c'] = ''
is possible?
for background, have config file contains dotted notation points place in json data. i'd utilize dotted notation string access specific info in json file. example, in config:
path_to_data = "user.name.first_name"
i'd script recognize as:
json_data["user"]["name"]["first_name"]
so can value of first_name field. converted original string list, , don't know how convert nested dict.
edit: there existing info construction need apply dict with. let's say:
m = {'a': {'b': {'c': 'lolcat'}}}
so that
m['a']['b']['c']
gives me 'lolcat'. if right dictionary construction (as of replies did), still need apply existing dictionary 'm'.
so, again, config file:
c = 'a.b.c'
that converted list, thinking create things easier:
x = ['a','b','c']
now have json-like info structure:
m = {'a': {'b': {'c': 'lolcat'}}}
so nested dict generated 'x' should able traverse 'm' that
m['a']['b']['c']
gets me cat.
li = ['a','b','c'] d = reduce(lambda x, y: {y:x}, reversed(li+[''])) print(d) print(d['a']['b']['c'])
python list dictionary
Comments
Post a Comment