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 November 29th, 2012, 01:59 PM
green_leaf green_leaf is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 3 green_leaf User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 m 36 sec
Reputation Power: 0
Print nested structures

Hello guys,
I have a quick question.
Why does the code below

Code:
structOutput = {'Contribution_from_sale': 'some val', 'Marginal_in_AIR': 'some val', 'Marginal_bad_debt': 'some val', 'Total_financial_effect': 'some val'}
listProducts = {'Product_1':structOutput, 'Product_2':structOutput, 'Product_3':structOutput}
listClients = {'Client_1':listProducts, 'Client_2':listProducts, 'Client_3':listProducts}
 

for names,lists in listClients.items():
    print names
    for prods,vals in listClients[names].items():
        print prods
        print listClients[names][prods]   


not print the structures in order, but instead

Code:
Client_1
Product_1
{'Total_financial_effect': 'some val', 'Contribution_from_sale': 'some val', 'Marginal_bad_debt': 'some val', 'Marginal_in_AIR': 'some val'}
Product_3
{'Total_financial_effect': 'some val', 'Contribution_from_sale': 'some val', 'Marginal_bad_debt': 'some val', 'Marginal_in_AIR': 'some val'}
Product_2
{'Total_financial_effect': 'some val', 'Contribution_from_sale': 'some val', 'Marginal_bad_debt': 'some val', 'Marginal_in_AIR': 'some val'}
Client_2
Product_1
{'Total_financial_effect': 'some val', 'Contribution_from_sale': 'some val', 'Marginal_bad_debt': 'some val', 'Marginal_in_AIR': 'some val'}
Product_3
{'Total_financial_effect': 'some val', 'Contribution_from_sale': 'some val', 'Marginal_bad_debt': 'some val', 'Marginal_in_AIR': 'some val'}
Product_2
{'Total_financial_effect': 'some val', 'Contribution_from_sale': 'some val', 'Marginal_bad_debt': 'some val', 'Marginal_in_AIR': 'some val'}
Client_3
Product_1
{'Total_financial_effect': 'some val', 'Contribution_from_sale': 'some val', 'Marginal_bad_debt': 'some val', 'Marginal_in_AIR': 'some val'}
Product_3
{'Total_financial_effect': 'some val', 'Contribution_from_sale': 'some val', 'Marginal_bad_debt': 'some val', 'Marginal_in_AIR': 'some val'}
Product_2
{'Total_financial_effect': 'some val', 'Contribution_from_sale': 'some val', 'Marginal_bad_debt': 'some val', 'Marginal_in_AIR': 'some val'} 

Reply With Quote
  #2  
Old November 29th, 2012, 02:26 PM
rrashkin's Avatar
rrashkin rrashkin is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Location: 39N 104.28W
Posts: 90 rrashkin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 13 h 37 m 44 sec
Reputation Power: 2
because dictionaries are not ordered (they're mapped). See the library reference, Mapping Types

Quote:
Keys and values are listed in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary’s history of insertions and deletions.

Reply With Quote
  #3  
Old November 29th, 2012, 02:48 PM
green_leaf green_leaf is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 3 green_leaf User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 m 36 sec
Reputation Power: 0
I see...
So then printing them in the order i have writen them can not be done with this kind of structure from what i understand right?

Reply With Quote
  #4  
Old November 29th, 2012, 03:04 PM
rrashkin's Avatar
rrashkin rrashkin is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Location: 39N 104.28W
Posts: 90 rrashkin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 13 h 37 m 44 sec
Reputation Power: 2
not without a bit of forethought in the structure of the keys (so they might be sorted).

Reply With Quote
  #5  
Old November 29th, 2012, 03:12 PM
AndrewSW's Avatar
AndrewSW AndrewSW is offline
JavaScript is not spelt java
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2011
Location: Landan, England
Posts: 743 AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 22 h 56 m
Reputation Power: 164
One way is:

Code:
for key in sorted(a_dict.keys()):
    print(a_dict[key])

Reply With Quote
  #6  
Old November 29th, 2012, 03:15 PM
green_leaf green_leaf is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 3 green_leaf User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 m 36 sec
Reputation Power: 0
I think i am starting to get it thanks guys...

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Print nested structures

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