How to sort a python dictionary in descending order?

Asked

Viewed 235 times

1

How can I order this dictionary so that it is in descending order:

 dict= {'1': array([44, 32, 56, 57, 43, 21, 36, 35, 39, 27, 23, 24, 25, 26, 31, 28, 29,
           30, 20, 22, 18, 19,  8,  1, 33,  2,  3,  4,  0,  6,  7,  9, 17, 10,
           11, 12, 50, 55, 13, 14, 15, 16, 52, 46, 45, 34, 42, 37, 40,  5, 54,
           51, 53, 48, 38, 41, 49, 47], dtype=int64), '2': array([50, 27, 39, 44, 47, 35, 28, 25, 37, 36, 29, 20, 21, 22, 13,  9, 18,
            6,  2, 14, 15, 16,  4,  7, 24, 19,  1,  5,  8, 17, 12, 11,  0,  3,
           10, 53, 49, 42, 55, 57, 45, 56, 54, 38, 23, 26, 31, 52, 51, 43, 33,
           46, 48, 40, 30, 34, 32, 41], dtype=int64), '3': array([54, 29, 40, 42, 53, 23, 31, 34, 37, 39, 38, 17, 19, 22, 12, 10, 11,
            3,  6, 13, 15,  8, 14, 16, 25, 27,  4,  5, 21,  0,  1, 18,  9,  7,
            2, 32, 28, 46, 48, 57, 56, 55, 20, 44, 33, 26, 41, 52, 24, 45, 43,
           50, 51, 49, 35, 30, 47, 36], dtype=int64), '4': array([57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41,
           40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24,
           23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10,  9,  8,  7,
            6,  5,  4,  3,  2,  1,  0], dtype=int64)}

1 answer

1


I used the dictionary as follows:

dict= {'1': [44, 32, 56, 57, 43, 21, 36, 35, 39, 27, 23, 24, 25, 26, 31, 28, 29,
           30, 20, 22, 18, 19,  8,  1, 33,  2,  3,  4,  0,  6,  7,  9, 17, 10,
           11, 12, 50, 55, 13, 14, 15, 16, 52, 46, 45, 34, 42, 37, 40,  5, 54,
           51, 53, 48, 38, 41, 49, 47], '2': [50, 27, 39, 44, 47, 35, 28, 25, 37, 36, 29, 20, 21, 22, 13,  9, 18,
            6,  2, 14, 15, 16,  4,  7, 24, 19,  1,  5,  8, 17, 12, 11,  0,  3,
           10, 53, 49, 42, 55, 57, 45, 56, 54, 38, 23, 26, 31, 52, 51, 43, 33,
           46, 48, 40, 30, 34, 32, 41]}

And I used it here to sort..

{k: v for k, v in sorted(dict.items(), key=lambda item: item[1], reverse=True)}

Upshot:

{'2': [50,  27,  39,  44,  47,  35,  28,  25,  37,  36,  29,  20,  21,  22,  13,  9,  18,  6,  2,  14,  15,  16,  4,  7,  24,  19,  1,  5,  8,  17,  12,  11,  0,  3,  10,  53,  49,
  42,  55,  57,  45,  56,  54,  38,  23,  26,  31,  52,  51,  43,  33,  46,  48,  40,  30,
  34,  32,  41], 
'1': [44,  32,  56,  57,  43,  21,  36,  35,  39,  27,  23,  24,  25,
  26,  31,  28,  29,  30,  20,  22,  18,  19,  8,  1,  33,  2,  3,  4,  0,  6,  7,  9,
  17,  10,  11,  12,  50,  55,  13,  14,  15,  16,  52,  46,  45,  34,  42,  37,  40,  5,
  54,  51,  53,  48,  38,  41,  49,  47]}

If you want it in the opposite order, just put Reverse as FALSE

EXAMPLE WITH PREVIOUSLY CREATED VARIABLE

a = [44, 32, 56, 57, 43, 21, 36, 35, 39, 27, 23, 24, 25, 26, 31, 28, 29,
           30, 20, 22, 18, 19,  8,  1, 33,  2,  3,  4,  0,  6,  7,  9, 17, 10,
           11, 12, 50, 55, 13, 14, 15, 16, 52, 46, 45, 34, 42, 37, 40,  5, 54,
           51, 53, 48, 38, 41, 49, 47]

dict= {'1': a, '2': [50, 27, 39, 44, 47, 35, 28, 25, 37, 36, 29, 20, 21, 22, 13,  9, 18,
            6,  2, 14, 15, 16,  4,  7, 24, 19,  1,  5,  8, 17, 12, 11,  0,  3,
           10, 53, 49, 42, 55, 57, 45, 56, 54, 38, 23, 26, 31, 52, 51, 43, 33,
           46, 48, 40, 30, 34, 32, 41], '3': [54, 29, 40, 42, 53, 23, 31, 34, 37, 39, 38, 17, 19, 22, 12, 10, 11,
            3,  6, 13, 15,  8, 14, 16, 25, 27,  4,  5, 21,  0,  1, 18,  9,  7,
            2, 32, 28, 46, 48, 57, 56, 55, 20, 44, 33, 26, 41, 52, 24, 45, 43,
           50, 51, 49, 35, 30, 47, 36], '4': [57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41,
           40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24,
           23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10,  9,  8,  7,
            6,  5,  4,  3,  2,  1,  0]}

{k: v for k, v in sorted(dict.items(), key=lambda item: item[1], reverse=True)}
  • I’m getting this error trying like this: Valueerror: The Truth value of an array with more than one element is ambiguous.

  • do not need to put array([]) on each item, leave as I showed in the example I assembled

  • I have a problem then, because this dictionary returns from another part of the code and it’s returning me this way. You know what you can do in this case?

  • From what I understand, it’s a dictionary composed of multiple INT vectors, correct? In this case there is no problem, the question is, why is Voce using the name array to inform that they are vectors? I’ll add another example in the little answer to see if it helps

  • The problem is that I don’t use the name array to inform that they are vectors, this is returned this way and I don’t understand the reason. This ends up impacting on that error. What I understand is that it is a dictionary that contains an array of int64 arrays. Each int64 in the arrays within Dict corresponds to a value of another variable. But I need to sort it in descending order and I’ve tried it in various ways that went wrong =(

  • I understood well then it gets complicated, because the way I already rode would need to pass in a routine that will take that name (Array) from there, but I do not know if Voce could do that, because of continuation of the code..

  • 1

    This worked for what I wanted to put 0 in the item: {k: v for k, v in Sorted(Dict.items(), key=lambda item: item[0], Reverse=True)}

  • great idea Patricia! Good that worked.. sorry the delay could not continue helping yesterday

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.