Quote:
| Originally Posted by sepp2k1 Do you have some control over the structure? Your problem would be much easier to solve if you had a dictionary of dictionaries, rather than a dictionary of lists of dictionaries, and there doesn't really seem to be any reason why you'd need a list of single-entry dictionaries rather than a dictionary with multiple entries. |
Thanks for the quick reply!
I do have control over how the structure is built and can change it although I may need some advice for that.
The above example I provided in the OP was contrived from my actual data to make the question a bit more clear. But here is actually what I am doing. I'm actually pulling the data from a web service call using suds. The call returns the following structure which I cannot alter.
[(mergedDefectDataObj){
checkerName = "NULL_RETURNS"
checkerSubcategory = "unimpl"
cid = 49473
componentName = "Default.Other"
defectStateAttributeValues[] =
(defectStateAttributeValueDataObj){
attributeDefinitionId =
(attributeDefinitionIdDataObj){
name = "Provenance"
}
attributeValueId =
(attributeValueIdDataObj){
name = "Parent - Baseline"
}
},
(defectStateAttributeValueDataObj){
attributeDefinitionId =
(attributeDefinitionIdDataObj){
name = "DefectStatus"
}
attributeValueId =
(attributeValueIdDataObj){
name = "New"
}
},
(defectStateAttributeValueDataObj){
attributeDefinitionId =
(attributeDefinitionIdDataObj){
name = "Classification"
}
attributeValueId =
(attributeValueIdDataObj){
name = "Unclassified"
}
},
domain = "STATIC_C"
filePathname = "/media/3TB/arkster/work/install/Build/gtk_Val/gtk+-2.14.2/gtk/gtkfilechooserbutton.c"
},
(mergedDefectDataObj){
checkerName = "NULL_RETURNS"
checkerSubcategory = "unimpl"
cid = 38796
componentName = "Default.Other"
defectStateAttributeValues[] =
(defectStateAttributeValueDataObj){
attributeDefinitionId =
(attributeDefinitionIdDataObj){
name = "Provenance"
}
attributeValueId =
(attributeValueIdDataObj){
name = "Parent - Baseline"
}
},
(defectStateAttributeValueDataObj){
attributeDefinitionId =
(attributeDefinitionIdDataObj){
name = "DefectStatus"
}
attributeValueId =
(attributeValueIdDataObj){
name = "New"
}
},
(defectStateAttributeValueDataObj){
attributeDefinitionId =
(attributeDefinitionIdDataObj){
name = "Classification"
}
attributeValueId =
(attributeValueIdDataObj){
name = "Unclassified"
}
},
domain = "STATIC_C"
filePathname = "/media/3TB/arkster/work/install/Build/gtk_Val/gtk+-2.14.2/modules/printbackends/lpr/gtkprintbackendlpr.c"
},
...
]
The above is the output for self.getMergedDefectsForStreamsResponse.mergedDefects which I then process into individual dictionaries that are appended into a list using setdefault with the 'cid' being the main dictionary key. Here is the code that does that.
for mergedDefect in self.getMergedDefectsForStreamsResponse.mergedDefects:
for i in mergedDefect.defectStateAttributeValues:
if i.attributeValueId == '':
continue
self.mergedDefectDict.setdefault(mergedDefect.cid, []).append({i.attributeDefinitionId['name']:i.attributeValueId['name']})
self.mergedDefectDict.setdefault(mergedDefect.cid, []).append({ "componentName" : mergedDefect.componentName})
self.mergedDefectDict.setdefault(mergedDefect.cid, []).append({ 'checkerName' :mergedDefect.checkerName})
self.mergedDefectDict.setdefault(mergedDefect.cid, []).append({ 'domain' : mergedDefect.domain})
self.mergedDefectDict.setdefault(mergedDefect.cid, []).append({ 'filePathName' : mergedDefect.filePathname})
The above then creates the structure that I end up with.
Dict = {
49473L: [
{Provenance: Parent - Baseline},
{DefectStatus: New},
{Classification: Unclassified},
{Comment: "Please Fix." },
{'componentName': Default.Other},
{'checkerName': NULL_RETURNS},
{'domain': STATIC_C},
{'filePathName': /media/3TB/arkster/work/install/Build/gtk_Val/gtk+-2.14.2/gtk/gtkfilechooserbutton.c}
],
38796L: [
{Provenance: Parent - Baseline},
{DefectStatus: New},
{Classification: Unclassified},
{Comment: "False Positive. Please ignore"},
{'componentName': Default.Other},
{'checkerName': NULL_RETURNS},
{'domain': STATIC_C},
{'filePathName': /media/3TB/arkster/work/install/Build/gtk_Val/gtk+-2.14.2/modules/printbackends/lpr/gtkprintbackendlpr.c}
],
...
}
Is there a way that I can manage the above using a dictionary of dictionaries instead of using setdefault to pile stuff into a list?
I can provide more context if needed. Thanks much for the help!