Python词典列表的词典 [英] Python Dictionary of lists of dictionaries
问题描述
我希望能够从文件中访问数据,将其组织成一个我认为应该是字典列表的字典的结构,根据我的理解,可能如下所示:
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'...]
}
我不知道这是否是理想的正确的,但实际上我可以使用如下命令来访问字典的数据: ['ID_FILE '] ['KEY_1'] ['R_2'] ['A_3'] ['P_2']。b
,例如(使用方括号来选择字典的键,在括号内)知道 b
值 P_2
键,对于 A_3
R_2
KEY_1
或 ['ID_FILE'] ['KEY_1 '] .R_2
,并获得一个列表 ['A_1','A_2','A_3']
,即选择字典的键,表示,在结束,所需的信息,以获得最终的输出...
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 ...
结构相当复杂,但概念是可概括的,我会通过什么方法,python模块或最佳方法,我可以在python中实现这一点;如果最好从外部或从内部开始组织和连接字典到独特的大字典...
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...
编辑1: strong>也就是说,使用前一个键的值作为新的子字典的键。
Edit 1: That is, use the value of previous key as key for a new sub dictionary.
编辑2:源文件是CIF文件,如此(它是一个大文件,170925行)。考虑到cif文件,我正在分析从ATOM开始的行,将它们分成列:
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
所以,例如,要获得原子N的x坐标(第10列) (列6)的残基271(第8列)的第3列(列3),给出类似['4tvx'] ['A'] ['271'] ['N']的命令coordX
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
推荐答案
感谢Steven Rumbalski向我指出这个工具, PyCifRW ,但是,回答我的主题的问题,所需的结构是字典的字典,以实现这个gool:
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': {}
}
}
"""
这篇关于Python词典列表的词典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!