1

Como posso ordenar esse dicionário de forma que fique em ordem decrescente:

 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 Resposta 1

1

Utilizei o dicionário da seguinte forma:

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]}

E utilizei isso aqui para ordenar..

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

Resultado:

{'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]}

Caso queira em ordem contrária basta colocar o reverse como FALSE

EXEMPLO COM VARIAVEL JÁ CRIADA ANTERIORMENTE

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)}
8
  • Estou recebendo este erro ao tentar assim: ValueError: The truth value of an array with more than one element is ambiguous. Commented 1/04/2020 às 18:03
  • não precisa colocar array([]) em cada item, deixa como eu mostrei no exemplo que montei Commented 1/04/2020 às 18:08
  • Tenho um problema então, porque esse dicionário retorna de uma outra parte do código e ele está me retornando desta forma. Sabe o que é possível fazer neste caso? Commented 1/04/2020 às 18:11
  • Pelo que entendi, é um dicionário composto por vários vetores de INT, correto? nesse caso não tem problema, a questão é, porque voce está utilizando o nome array para informar que são vetores? vou adicionar um outro exemplo na inha resposta para ver se ajuda Commented 1/04/2020 às 18:16
  • 1
    Isso funcionou para o que eu queria se colocar 0 no item: {k: v for k, v in sorted(dict.items(), key=lambda item: item[0], reverse=True)} Commented 2/04/2020 às 2:30

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .