Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 21st, 2013, 11:42 AM
arkster arkster is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 3 arkster User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 m
Reputation Power: 0
Dictionaries containing list that contain dictionaries

Please bear with me if I am not able to explain this clearly. I'm new to python programming although I've done some programming in perl previously. I'm kind of stuck trying to figure out how to extract some data from the following structure.


abc = {
7777 : [ {"name":"ark"}, { "city":"palo alto"},{ "tel":"123456789"} ],
8888 : [ {"name": "pinky"}, {"city":"palo alto"}, {"tel" : "987654321"} ],
9999 : [ {"name": "joey"}, {"city":"los altos"}, {"tel" : "234543213"} ],
...
}


How can I pull the value for the "tel" key inside the lists above where the "city" matches "palo alto"? I can print the lists and even indices of the lists using some for loops but I'm having a difficult time figuring out how to extract or match the 'city' in an if statement.

Thanks!

Reply With Quote
  #2  
Old March 21st, 2013, 12:06 PM
sepp2k1 sepp2k1 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Posts: 75 sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 23 h 20 m 52 sec
Reputation Power: 38
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.

Reply With Quote
  #3  
Old March 21st, 2013, 01:54 PM
arkster arkster is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 3 arkster User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 m
Reputation Power: 0
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!

Reply With Quote
  #4  
Old March 21st, 2013, 10:13 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,361 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 10 h 11 sec
Reputation Power: 383
Without knowing what these methods do, I'll guess the following might give a dictionary of dictionaries.
Code:
for mergedDefect in self.getMergedDefectsForStreamsResponse.mergedDefects:
    for i in mergedDefect.defectStateAttributeValues:
        if i.attributeValueId:
            d = {i.attributeDefinitionId['name']:i.attributeValueId['name'],
                 'componentName' : mergedDefect.componentName,
                 'checkerName' :mergedDefect.checkerName,
                 'domain' : mergedDefect.domain,
                 'filePathName' : mergedDefect.filePathname,
                 }
            self.mergedDefectDict.setdefault(mergedDefect.cid, {}).update(d)


might give a dictionary with access
Code:
abc = { 
    7777 :  {"name":"ark", "city":"palo alto", "tel":"123456789"},
    8888 :  {"name": "pinky", "city":"palo alto", "tel" : "987654321"},
    9999 :  {"name": "joey", "city":"los altos", "tel" : "234543213"},
}

for (k,v,) in abc.items():  # if k not used, for v in abc.values():
    if v['city'] == 'palo alto':
        print(v['tel'])
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #5  
Old March 22nd, 2013, 10:10 AM
arkster arkster is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 3 arkster User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 m
Reputation Power: 0
Quote:
Originally Posted by b49P23TIvg
Without knowing what these methods do, I'll guess the following might give a dictionary of dictionaries.
Code:
for mergedDefect in self.getMergedDefectsForStreamsResponse.mergedDefects:
    for i in mergedDefect.defectStateAttributeValues:
        if i.attributeValueId:
            d = {i.attributeDefinitionId['name']:i.attributeValueId['name'],
                 'componentName' : mergedDefect.componentName,
                 'checkerName' :mergedDefect.checkerName,
                 'domain' : mergedDefect.domain,
                 'filePathName' : mergedDefect.filePathname,
                 }
            self.mergedDefectDict.setdefault(mergedDefect.cid, {}).update(d)


might give a dictionary with access
Code:
abc = { 
    7777 :  {"name":"ark", "city":"palo alto", "tel":"123456789"},
    8888 :  {"name": "pinky", "city":"palo alto", "tel" : "987654321"},
    9999 :  {"name": "joey", "city":"los altos", "tel" : "234543213"},
}

for (k,v,) in abc.items():  # if k not used, for v in abc.values():
    if v['city'] == 'palo alto':
        print(v['tel'])

Thank you! This is just what I needed.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Dictionaries containing list that contain dictionaries

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap