Sort dictionary by value and use rule if value is first python [duplicate]

0

I want to make a dictionary in python in which it will have diseases and scores ex: dic ['alzhelmer'] = 3 Dic ['flu'] = 5

The program should sort the diseases that scored the least to the least (descending) in the dictionary ex:

Dec ['flu'] = 5 Dec ['alzhelmer'] = 3

And finally if for example flu is the most punctuate, give a command.

ex: if dic ['flu'] == biggestpoint:

    "url= gripe.com"

Thanks in advance!

    
asked by anonymous 09.09.2017 / 20:26

1 answer

0

Dictionaries in python are not "orderly". There are several other python-classable objects, so I suggest you use one of them, and then transfer it to an OrderedDict dictionary (which, unlike an ordinary dictionary, keeps the order it was created each time you access it) p>

from collections import OrderedDict

# Criando pontuações em lista de tuplas (poderia ser lista de listas)
data = [('Asma', 6), ('Cachumba', 5), ('Difteria', 4),('Rubeola', 9)]

# Criando o dicionario
od = OrderedDict([par for index, par in sorted((tpl[1], tpl) for tpl in data)])

print (od)
OrderedDict([('Difteria', 4), ('Cachumba', 5), ('Asma', 6), ('Rubeola', 9)])

print(od['Cachumba'])
5
  

Edited
  If you want reverse order:

# Criando pontuações em lista de tuplas (poderia ser lista de listas)
data = [('Asma', 6), ('Cachumba', 5), ('Difteria', 4),('Rubeola', 9)]

# Criando a indexação na ordem reversa
dr = sorted([(tpl[1], tpl) for tpl in data], reverse=True)

# Criando o dicionário
od = OrderedDict([tpl[1] for tpl in dr])

print (od)
OrderedDict([('Rubeola', 9), ('Asma', 6), ('Cachumba', 5), ('Difteria', 4)])

Note:
If your list is already sorted, then just create the dictionary (OrderedDict) normally. As for the "command" question at the end of the question, I could not understand, the purpose of the dictionaries is to only map keys / values.

    
09.09.2017 / 21:46