-1

The accepted answer of this post clearly mentions that it's not possible to sort a dictionary in Python. However, the next answer claims that it is actually possible to do a "sort by dictionary values". My situation is close to this post. Here, the main dictionary is suggested to sort by a value of a key of one of the inner dictionaries using OrderedDict. In my case, I want the key of the main dictionary be intact but the values (which are dictionaries itself) to be sorted in descending order. I have the following dictionary:

    dict = {
        1: {1: -8, 2: -5, 3: -6, 4: -2},
        2: {1: -7, 2: -6, 3: -5, 4: -1}, 
        3: {1: -7, 2: -6, 3: -4, 4: -5}, 
        4: {1: -7, 2: -4, 3: -6, 4: -8}
       }

How can I arrange the inner dictionaries such that the resulting dictionary becomes as follows:

dict = {
        1: {4: -2, 2: -5, 3: -6, 1: -8},
        2: {4: -1, 3: -5, 2: -6, 1: -7}, 
        3: {3: -4, 4: -5, 2: -6, 1: -7}, 
        4: {2: -4, 3: -6, 1: -7, 4: -8}
       }

The installed python in my system is Python 3.6.5. It has become just a week since I ran my first python code. Hence, I'm very new to python. I had asked few questions before, which didn't receive a good response. I had to delete a question too. I'm in danger of being blocked for asking any more questions. I saw many posts regarding the sorting of a dictionary in python and the mixed answers has confused me. Basically, I am asking to sort the keys of the inner dictionary by their values (without changing the main dictionary's key order). Is it even possible? How can I do it? Please help me!

Community
  • 1
  • 1
santobedi
  • 866
  • 3
  • 17
  • 39
  • Dictionaries are inherently unordered (pre Python 3.7). Use a different data structure if you care about order – juanpa.arrivillaga Apr 25 '18 at 02:12
  • As of Python 3.6, for the CPython implementation of Python, dictionaries remember the order of items inserted. The order-preserving aspect of this new implementation is considered an implementation detail and should not be relied upon – johnashu Apr 25 '18 at 02:38
  • 1
    Your best bet would be to use SortedContainer's SortedDict: http://www.grantjenks.com/docs/sortedcontainers/sorteddict.html You can provide a key argument(not to be confused with the keys of your dictionary) by which to sort the keys of your SortedDicts. Otherwise you may as well just explain what you are trying to do and we could surely give you an alternative to nested dictionaries. – Travis Black Apr 25 '18 at 03:17
  • @TravisBlack In the real case, the inner dictionaries contain many key-value pairs. After sorting them in descending order, the top 'n' key-value pairs are needed for further operation. 'N' being the total number of key-value pairs, 'N-n' pairs are discarded. However, the keys of outer dictionary must remain intact. – santobedi Apr 25 '18 at 04:29

1 Answers1

0

I wanted to sort the keys of inner dictionaries by their values. I could sort the keys by their values, however, the output is different (list inside the dictionary) but useful in my case.

dict1 = {}
for Keys, Values in dict.items():
   dict1[Keys] = sorted(Values, key = Values.get)

While printing the dict1, I get as follows:

dict1 = {
         1: [4, 1, 3, 2], 
         2: [1, 4, 3, 2], 
         3: [1, 4, 2, 3], 
         4: [4, 1, 2, 3]
        }

Though I wanted my output in a nested dictionary, I'll modify my code the fit to this result.

santobedi
  • 866
  • 3
  • 17
  • 39