Translate a table to a hierarchical dictionary?

2019-02-17 19:08发布

I have a table of the form:

A1, B1, C1, (value)
A1, B1, C1, (value)
A1, B1, C2, (value)
A1, B2, C1, (value)
A1, B2, C1, (value)
A1, B2, C2, (value)
A1, B2, C2, (value)
A2, B1, C1, (value)
A2, B1, C1, (value)
A2, B1, C2, (value)
A2, B1, C2, (value)
A2, B2, C1, (value)
A2, B2, C1, (value)
A2, B2, C2, (value)
A2, B2, C2, (value)

I'd like to work with it in python as a dictionary, of form:

H = {
    'A1':{
        'B1':{
            'C1':[],'C2':[],'C3':[] },
        'B2':{
            'C1':[],'C2':[],'C3':[] },
        'B3':{
            'C1':[],'C2':[],'C3':[] }
    },
    'A2':{
        'B1':{
            'C1':[],'C2':[],'C3':[] },
        'B2':{
            'C1':[],'C2':[],'C3':[] },
        'B3':{
            'C1':[],'C2':[],'C3':[] }
    }
}

So that H[A][B][C] yields a particular unique list of values. For small dictionaries, I might just define the structure in advance as above, but I am looking for an efficient way to iterate over the table and build a dictionary, without specifying the dictionary keys ahead of time.

3条回答
Summer. ? 凉城
2楼-- · 2019-02-17 19:48

If you ever only access H[A][B][C] (that is, never H[A] oder H[A][B] alone), I'd suggest a IMO cleaner solution: Use Tuples as defaultdict Index:

from collections import defaultdict
h = defaultdict(list)
for a, b, c, value in input:
    h[a, b, c].append(value)
查看更多
ゆ 、 Hurt°
3楼-- · 2019-02-17 19:54
input = [('A1', 'B1', 'C1', 'Value'), (...)]

from collections import defaultdict

tree = defaultdict(lambda: defaultdict(lambda: defaultdict(list)))
#Alternatively you could use partial() rather than lambda:
#tree = defaultdict(partial(defaultdict, partial(defaultdict, list)))

for x, y, z, value in input:
    tree[x][y][z].append(value)
查看更多
别忘想泡老子
4楼-- · 2019-02-17 20:04
d = {}
for (a, b, c, value) in your_table_of_tuples:
   d.setdefault(a, {}).setdefault(b,{}).setdefault(c,[]).append(value)
查看更多
登录 后发表回答