I need to sort a dictionary by first, values with reverse=True
, and for repeating values, sort by keys, reverse=False
So far, I have this
dict = [('B', 3), ('A', 2), ('A', 1), ('I', 1), ('J', 1)]
sorted(dict.items(), key=lambda x: (x[1],x[1]), reverse=True)
which returns...
[('B', 3), ('A', 2), ('J', 1), ('I', 1), ('A', 1)]
but I need it to be:
[('B', 3), ('A', 2), ('A', 1), ('I', 1), ('J', 1)]
as you can see, when values are equal, I can only sort the key in a decreasing fashion as specified... But how can I get them to sort in an increasing fashion?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…