我有一个字典列表(MyList),在某些情况下它们将嵌套数组作为值。我试图遍历列表并在每个字典上应用递归函数(flat)以展平它们,然后将新的平面字典附加到另一个列表中。
我的问题是,生成的字典似乎建立在之前的迭代之上,并保留了之前扁平的dicts中不常见的键值对。我想在输出中为每个字典提供原始键值对。
任何指针都会非常感激。
MyList = [
{
"altLabel_attr": [
"hA20",
"IMMU-106",
"Veltuzumab",
"VELTUZUMAB"
],
"chemblId_attr": "CHEMBL1743088",
"description_attr": "Veltuzumab (humanized mab)",
"hasExternalLink": False,
"internalDocument": True,
"isBiotherapeutic_attr": True,
"isImageMap": False,
"label": "VELTUZUMAB",
"label_attr": "VELTUZUMAB",
"prefLabel_attr": "VELTUZUMAB",
"score": 1,
"subType": "",
"subjectUris": [
"http://rdf.ebi.ac.uk/resource/chembl/molecule/CHEMBL1743088"
],
"substanceType_attr": "Antibody",
"type": "target"
},
{
"altLabel_attr": [
"PRONASE",
"Pronase"
],
"chemblId_attr": "CHEMBL2108476",
"hasExternalLink": False,
"internalDocument": True,
"isImageMap": False,
"label": "PRONASE",
"label_attr": "PRONASE",
"prefLabel_attr": "PRONASE",
"score": 1,
"subType": "",
"subjectUris": [
"http://rdf.ebi.ac.uk/resource/chembl/molecule/CHEMBL2108476"
],
"substanceType_attr": "Enzyme",
"type": "target"
},
{
"altLabel_attr": "MDX-1342",
"chemblId_attr": "CHEMBL2109337",
"hasExternalLink": False,
"internalDocument": True,
"isBiotherapeutic_attr": True,
"isImageMap": False,
"label": "MDX-1342",
"label_attr": "MDX-1342",
"prefLabel_attr": "MDX-1342",
"score": 1,
"subType": "",
"subjectUris": [
"http://rdf.ebi.ac.uk/resource/chembl/molecule/CHEMBL2109337"
],
"substanceType_attr": "Antibody",
"type": "target"
}
]
这是我的扁平化功能:
def flat(structure, key="", path="", flattened={}):
if not isinstance(structure,(dict, list)):
flattened[((path + "_") if path else "") + key] = structure
elif isinstance(structure, list):
for i, item in enumerate(structure):
flat(item,str(i), "_".join(filter(None,[path,key])), flattened)
else:
for new_key, value in structure.iteritems():
flat(value, new_key, "*".join(filter(None,[path,key])), flattened)
return flattened
ListFlatDicts = []
for i in MyList:
i = dict(flat(i))
ListFlatDicts.append(i)
for i in ListFlatDicts:
print i
当前输出:
{'altLabel_attr_3': 'VELTUZUMAB', 'altLabel_attr_2': 'Veltuzumab', 'altLabel_attr_1': 'IMMU-106', 'isImageMap': 'false', 'subjectUris_0': 'CHEMBL1743088', 'subType': '', 'internalDocument': 'true', 'label_attr': 'VELTUZUMAB', 'prefLabel_attr': 'VELTUZUMAB', 'substanceType_attr': 'Antibody', 'isBiotherapeutic_attr': 'true', 'altLabel_attr_0': 'hA20', 'hasExternalLink': 'false', 'label': 'VELTUZUMAB', 'score': 1, 'description_attr': 'Veltuzumab (humanized mab)', 'chemblId_attr': 'CHEMBL1743088', 'type': 'target'}
{'altLabel_attr_3': 'VELTUZUMAB', 'altLabel_attr_2': 'Veltuzumab', 'altLabel_attr_1': 'Pronase', 'isImageMap': 'false', 'subjectUris_0': 'CHEMBL2108476', 'subType': '', 'internalDocument': 'true', 'label_attr': 'PRONASE', 'prefLabel_attr': 'PRONASE', 'substanceType_attr': 'Enzyme', 'isBiotherapeutic_attr': 'true', 'altLabel_attr_0': 'PRONASE', 'hasExternalLink': 'false', 'label': 'PRONASE', 'score': 1, 'description_attr': 'Veltuzumab (humanized mab)', 'chemblId_attr': 'CHEMBL2108476', 'type': 'target'}
{'altLabel_attr_3': 'VELTUZUMAB', 'altLabel_attr_2': 'Veltuzumab', 'altLabel_attr_1': 'Pronase', 'isImageMap': 'false', 'subjectUris_0': 'CHEMBL2109337', 'subType': '', 'internalDocument': 'true', 'label_attr': 'MDX-1342', 'prefLabel_attr': 'MDX-1342', 'substanceType_attr': 'Antibody', 'isBiotherapeutic_attr': 'true', 'altLabel_attr_0': 'PRONASE', 'hasExternalLink': 'false', 'label': 'MDX-1342', 'altLabel_attr': 'MDX-1342', 'score': 1, 'description_attr': 'Veltuzumab (humanized mab)', 'chemblId_attr': 'CHEMBL2109337', 'type': 'target'}
期望的输出:
{'altLabel_attr_3': 'VELTUZUMAB', 'altLabel_attr_2': 'Veltuzumab', 'altLabel_attr_1': 'IMMU-106', 'isImageMap': 'false', 'subjectUris_0': 'CHEMBL1743088', 'subType': '', 'internalDocument': 'true', 'label_attr': 'VELTUZUMAB', 'prefLabel_attr': 'VELTUZUMAB', 'substanceType_attr': 'Antibody', 'isBiotherapeutic_attr': 'true', 'altLabel_attr_0': 'hA20', 'hasExternalLink': 'false', 'label': 'VELTUZUMAB', 'score': 1, 'description_attr': 'Veltuzumab (humanized mab)', 'chemblId_attr': 'CHEMBL1743088', 'type': 'target'},
{'prefLabel_attr': 'PRONASE', 'substanceType_attr': 'Enzyme', 'altLabel_attr_1': 'Pronase', 'isImageMap': 'false', 'subjectUris_0': 'CHEMBL2108476', 'altLabel_attr_0': 'PRONASE', 'hasExternalLink': 'false', 'label': 'PRONASE', 'subType': '', 'score': 1, 'internalDocument': 'true', 'chemblId_attr': 'CHEMBL2108476', 'type': 'target', 'label_attr': 'PRONASE'}
{'prefLabel_attr': 'MDX-1342', 'substanceType_attr': 'Antibody', 'isImageMap': 'false', 'isBiotherapeutic_attr': 'true', 'hasExternalLink': 'false', 'label': 'MDX-1342', 'altLabel_attr': 'MDX-1342', 'subType': '', 'score': 1, 'subjectUris_0': 'CHEMBL2109337', 'internalDocument': 'true', 'chemblId_attr': 'CHEMBL2109337', 'type': 'target', 'label_attr': 'MDX-1342'}