#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep 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'}
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    158
    Rep Power
    3
    because dictionaries are not ordered (they're mapped). See the library reference, Mapping Types

    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.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep 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?
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    158
    Rep Power
    3
    not without a bit of forethought in the structure of the keys (so they might be sorted).
  8. #5
  9. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    One way is:

    Code:
    for key in sorted(a_dict.keys()):
        print(a_dict[key])
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0
    I think i am starting to get it thanks guys...

IMN logo majestic logo threadwatch logo seochat tools logo