I have the following list of dictionaries:
[{'160': 1.598,
'1620': 1.305,
'26': 4.554,
'2952': 1.2380,
'2985': 1.56,
'3075': 1.04,
'3222': 0.9721,
'3388': 1.68,
'3443': 0.600,
'361': 2.6682,
'4': 5.22,
'601': 2.26,
'9': 5.032},
{'1457': 5.625,
'1469': 25.455,
'1470': 25.455,
'605': 5.74627},
{},
{'1258': 1.085,
'2571': 0.982,
'2943': 2.25,
'2951': 0.62,
'2953': 1.84,
'2958': 0.73,
'2959': 0.93,
'2978': 0.67,
'341': 1.039}]
I want to order each dictionary by the values in descending order, if two values are identical, priority is with the key which is smaller. It is possible that a dictionary be empty as above.
Here is my attempt, by it only does for the first one:
import operator
sorted(mylist[0].items(), key=operator.itemgetter(1),reverse=True)
I construct my list of dictionaries like this:
[{k:v for k,v in d.items() if v == max_vals[k]} for d in mylist
]
How can I do it inherently in this step? Without having to do the ordering in a separate line? How can the order be maintained withing the construction step?