输入:
a_list = [x,y,z,p,q,r]
value = 5
输出:
dict1 = {x: {y : {z : { p :{ q :{ r : 5}}}}}
答案 0 :(得分:1)
逐步构建嵌套字典,从单个值开始:
from functools import reduce
dict1 = reduce(lambda d,key: {key : d}, a_list[::-1], value)
print(dict1)
#{'x': {'y': {'z': {'p': {'q': {'r': 5}}}}}}