Python Dictionary of lists of dictionaries

2019-09-20 18:47发布

I would like to be able to access data from a file, organizing them into a structure that I think should be a dictionary of lists of dictionaries, from what I understand, probably like the following:

ID_FILE = { 
        'KEY_1': [
                    {
                        'R_1': [
                                {
                                    'A_1': [
                                            {
                                                'P_1': ['a', 'b', 'c', 'd']
                                            },
                                            {
                                                'P_2': ['a', 'b', 'c', 'd']
                                            },
                                            {
                                                'P_3': ['a', 'b', 'c', 'd']
                                            }
                                        ],
                                    'A_2': [
                                            {
                                                'P_1': ['a', 'b', 'c', 'd']
                                            },
                                            {
                                                'P_2': ['a', 'b', 'c', 'd']
                                            },
                                            {
                                                'P_3': ['a', 'b', 'c', 'd']
                                            }
                                        ],
                                    'A_3': [
                                            {
                                                'P_1': ['a', 'b', 'c', 'd']
                                            },
                                            {
                                                'P_2': ['a', 'b', 'c', 'd']
                                            },
                                            {
                                                'P_3': ['a', 'b', 'c', 'd']
                                            }
                                        ]
                                }
                            ],
                        'R_2': [
                                {
                                    'A_1': [
                                            {
                                                'P_1': ['a', 'b', 'c', 'd']
                                            },
                                            {
                                                'P_2': ['a', 'b', 'c', 'd']
                                            },
                                            {
                                                'P_3': ['a', 'b', 'c', 'd']
                                            }
                                        ],
                                    'A_2': [
                                            {
                                                'P_1': ['a', 'b', 'c', 'd']
                                            },
                                            {
                                                'P_2': ['a', 'b', 'c', 'd']
                                            },
                                            {
                                                'P_3': ['a', 'b', 'c', 'd']
                                            }
                                        ],
                                    'A_3': [
                                            {
                                                'P_1': ['a', 'b', 'c', 'd']
                                            },
                                            {
                                                'P_2': ['a', 'b', 'c', 'd']
                                            },
                                            {
                                                'P_3': ['a', 'b', 'c', 'd']
                                            }
                                        ]
                                }
                        ],
                        'R_3': [
                                {
                                    'A_1': [
                                            {
                                                'P_1': ['a', 'b', 'c', 'd']
                                            },
                                            {
                                                'P_2': ['a', 'b', 'c', 'd']
                                            },
                                            {
                                                'P_3': ['a', 'b', 'c', 'd']
                                            }
                                        ],
                                    'A_2': [
                                            {
                                                'P_1': ['a', 'b', 'c', 'd']
                                            },
                                            {
                                                'P_2': ['a', 'b', 'c', 'd']
                                            },
                                            {
                                                'P_3': ['a', 'b', 'c', 'd']
                                            }
                                        ],
                                    'A_3': [
                                            {
                                                'P_1': ['a', 'b', 'c', 'd']
                                            },
                                            {
                                                'P_2': ['a', 'b', 'c', 'd']
                                            },
                                            {
                                                'P_3': ['a', 'b', 'c', 'd']
                                            }
                                        ]
                                }
                        ]
                    }
                ],

        'KEY_2': [...as 'KEY_1'...],

        'KEY_3': [...as 'KEY_1'...]
    }

I do not know whether it is ideally correct, but in practice I would be able to access the dictionary's data with a command like ['ID_FILE']['KEY_1']['R_2']['A_3']['P_2'].b, for example (using square brackets to select dictionary's key, and a dot for the wanted value outside the brackets) to know the b value of P_2 key, for A_3 in R_2 of KEY_1, or ['ID_FILE']['KEY_1'].R_2 and get a list ['A_1', 'A_2' , 'A_3'], that is, select the dictionary's keys, indicating, at the end, the desired information, to get the final output ...

The structure is quite complex, but the concept is generalizable, and I would understand by what method, python module or best approach, I can achieve this in python; if it would be preferred to start from the outside or from within, to organize and concatenate dictionaries into the unique big dictionary...

Edit 1: That is, use the value of previous key as key for a new sub dictionary.

Edit 2: The source file is a CIF file like this (it's a big file with 170925 rows). Considering that cif file, I'm analyzing the lines starting with ATOM, dividing them into columns:

KEY = column 6
R = column 8
A = column 3
P = Atom row = list of columns
a = column 1
b = column 2
c = column 3
d = column 4

So, for example, to get the x coordinate (column 10) of atom N (column 3) of residue 271 (column 8) of chain A (column 6), give a command like that ['4tvx']['A']['271']['N'].coordX

2条回答
孤傲高冷的网名
2楼-- · 2019-09-20 19:04

It seems like your schema is not ideal (although it is difficult to tell without understanding what you are storing). From looking at your data, KEY_1, R_1, and A_1 should be dictionaries rather than lists. By simplifying your structure, you will be to use [ID_FILE][KEY_1][R_2][A_3][P_2].b

查看更多
Ridiculous、
3楼-- · 2019-09-20 19:08

Thanks to Steven Rumbalski to indicate to me that tool, PyCifRW, but, answering to my topic's question, the needed structure is a dictionary of dictionaries, to achieve that gool:

r_list = ['20', '21']
dictionary = {}
r_dict = {}
a_dict = {}
for r in range(0,len(r_list)):
    r = r_list[r]
    dictionary['C'] = r_dict
    r_dict[r] = a_dict

print dictionary
print dictionary['C']

"""output:

{'C': {'20': {}, '21': {}}}
{'20': {}, '21': {}}

equal to:

dictionary = {'C': {
                    '20': {},
                    '21': {}
                }
            }
"""
查看更多
登录 后发表回答