The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Print nested structures
Discuss Print nested structures in the Python Programming forum on Dev Shed. Print nested structures Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 29th, 2012, 01:59 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 3
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'}
|

November 29th, 2012, 02:26 PM
|
 |
Contributing User
|
|
Join Date: May 2012
Location: 39N 104.28W
Posts: 90
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. |
|

November 29th, 2012, 02:48 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 3
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?
|

November 29th, 2012, 03:04 PM
|
 |
Contributing User
|
|
Join Date: May 2012
Location: 39N 104.28W
Posts: 90
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).
|

November 29th, 2012, 03:12 PM
|
 |
JavaScript is not spelt java
|
|
Join Date: Feb 2011
Location: Landan, England
|
|
One way is:
Code:
for key in sorted(a_dict.keys()):
print(a_dict[key])
|

November 29th, 2012, 03:15 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 3
Time spent in forums: 14 m 36 sec
Reputation Power: 0
|
|
|
I think i am starting to get it thanks guys...
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|