Sort lists in dictionaries in python

2

Hello, I'm trying to sort my dictionary, but I'm finding it difficult.

dict = {'EstacaoCodigo' : ['1','2','3'] ,'NivelConsistencia' : ['0','2','1'] ,'Data' : ['01/12/1996','01/12/1999','01/12/1994'] }

Would it be possible to sort the list values from the list of dates? The expected result is:

{'EstacaoCodigo' : ['2','1','3'] ,'NivelConsistencia' : ['2','0','1'] ,'Data' : ['01/12/1999','01/12/1996','01/12/1994'] }
    
asked by anonymous 26.10.2016 / 19:34

1 answer

0

If you only have one dictionary and want to sort a list within that dictionary, you can use sorted , like in the example below:

>>> d = {'EstacaoCodigo' : ['1','2','3'] ,'NivelConsistencia' : ['0','2','1'] ,'Data' : ['01/12/1996','01/12/1999','01/12/1994'] }
>>> d['Data'] = sorted(d['Data'], reverse=True)
>>> d
{'EstacaoCodigo' : ['2','1','3'] ,'NivelConsistencia' : ['2','0','1'] ,'Data' : ['01/12/1999','01/12/1996','01/12/1994'] }

If you use other ways and expose the date, some things may have to be changed in the sorted function, but the sorted will probably still suffice, with at most having to add a value key .

    
26.10.2016 / 20:18