将多个列表映射到字典

ema*_*mad 3 python mapping dictionary list

我有5个列表,我想将它们映射到分层字典.

让我说我有:

temp = [25, 25, 25, 25]
volt = [3.8,3.8,3.8,3.8]
chan = [1,1,6,6]
rate = [12,14,12,14]
power = [13.2,15.3,13.8,15.1]
Run Code Online (Sandbox Code Playgroud)

我想要的是我的字典是这样的:

{25:{3.8:{1:{12:13.2,14:15.3},6:{12:13.8,14:15.1}}}}
Run Code Online (Sandbox Code Playgroud)

基本上字典结构是:

{temp:{volt:{chan:{rate:power}}}}
Run Code Online (Sandbox Code Playgroud)

我尝试使用zip函数但在这种情况下它没有帮助,因为顶层列表中的重复值

tob*_*s_k 8

这只是稍作测试,但似乎可以解决问题.基本上,什么f呢,是创建defaultdictdefaultdicts.

f = lambda: collections.defaultdict(f)
d = f()
for i in range(len(temp)):
    d[temp[i]][volt[i]][chan[i]][rate[i]] = power[i]
Run Code Online (Sandbox Code Playgroud)

例:

>>> print d[25][3.8][6][14]
15.1
Run Code Online (Sandbox Code Playgroud)

(这个想法借鉴了相关问题的答案.)