Apr-01-2017, 06:09 PM
What I have: a nested dict (see code), with integer values as lowest "level".
What I wish to reach: a couple of lists with pairs of the highest level key and the values, while the intermediate keys are 'transformed' in the name of each list. The last part is done manually. (In the output, the order of the lists among each other is not relevant, only the order of the list items.)
My code actually is something like:
The sorting doesn't work in this way. As far as I can say, the order of the keys in the result lists is arbitrary. But how to use the itemgetter (or something else) to sort the listdict by the second list item then?
And I wonder whether there would be a more straight way to achieve my goal?
What I wish to reach: a couple of lists with pairs of the highest level key and the values, while the intermediate keys are 'transformed' in the name of each list. The last part is done manually. (In the output, the order of the lists among each other is not relevant, only the order of the list items.)
My code actually is something like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
import operator dict_to_sort = { "key1" : { "subkey1" : { "subsubkey1" : 1 , "subsubkey2" : 9 }, "subkey2" : { "subsubkey1" : 4 , "subsubkey2" : 24 }, "subkey3" : { "subsubkey1" : 5 , "subsubkey2" : 7 }}, "key2" : { "subkey1" : { "subsubkey1" : 12 , "subsubkey2" : 4 }, "subkey2" : { "subsubkey1" : 226 , "subsubkey2" : 17 }, "subkey3" : { "subsubkey1" : 3 , "subsubkey2" : 3 }}, "key3" : { "subkey1" : { "subsubkey1" : 8 , "subsubkey2" : 11 }, "subkey2" : { "subsubkey1" : 105 , "subsubkey2" : 10 }, "subkey3" : { "subsubkey1" : 9 , "subsubkey2" : 10 }}} ##desired output: ##subkey3subsubkey1: [['key3', 9], ['key1', 5], ['key2', 3]] ## ##subkey1subsubkey1: [['key2', 12], ['key3', 8], ['key1', 1]] ## ##subkey2subsubkey2: [['key1', 24], ['key2', 17], ['key3', 10]] ## ##subkey1subsubkey2: [['key3', 11], ['key1', 9], ['key2', 4]] ## ##subkey3subsubkey2: [['key3', 10], ['key1', 7], ['key2', 3]] ## ##subkey2subsubkey1: [['key2', 226], ['key3', 105], ['key1', 4]] klist = [ "subkey1subsubkey1" , "subkey1subsubkey2" , "subkey2subsubkey1" , "subkey2subsubkey2" , "subkey3subsubkey1" , "subkey3subsubkey2" ] listdict = {} for lii in klist: listdict[lii] = [] for tup in [[ "subkey1subsubkey1" , "subkey1" , "subsubkey1" ], [ "subkey1subsubkey2" , "subkey1" , "subsubkey2" ], [ "subkey2subsubkey1" , "subkey2" , "subsubkey1" ], [ "subkey2subsubkey2" , "subkey2" , "subsubkey2" ], [ "subkey3subsubkey1" , "subkey3" , "subsubkey1" ], [ "subkey3subsubkey2" , "subkey3" , "subsubkey2" ]]: for keynum in dict_to_sort.keys(): listdict[tup[ 0 ]].append([keynum, dict_to_sort[keynum][tup[ 1 ]][tup[ 2 ]]]) for liik in listdict.keys(): sorted (listdict, key = operator.itemgetter( 1 ), reverse = True ) print ( "\n" + liik + ": " + str (listdict[liik])) |
The sorting doesn't work in this way. As far as I can say, the order of the keys in the result lists is arbitrary. But how to use the itemgetter (or something else) to sort the listdict by the second list item then?
And I wonder whether there would be a more straight way to achieve my goal?