mapping multiple lists to dictionary

前端 未结 2 1556
死守一世寂寞
死守一世寂寞 2021-01-20 17:32

I have 5 lists and I want to map them to a hierarchical dictionary.

let\'s say i have:

temp = [25, 25, 25, 25]
volt = [3.8,3.8,3.8,3.8]
chan = [1,1,6         


        
2条回答
  •  轮回少年
    2021-01-20 18:01

    This is only slightly tested, but it seems to do the trick. Basically, what f does, is to create a defaultdict of defaultdicts.

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

    Example:

    >>> print d[25][3.8][6][14]
    15.1
    

    (The idea is borrowed from this answer to a related question.)

提交回复
热议问题