简体   繁体   中英

Python sort dictionary by descending values and then by keys alphabetically

I have the following dictionary:

fruits = {
    "apple": 5,
    "Apple": 5,
    "orange": 5,
    "strawberry": 3,
    "blueberry": 1
}

I need to print out a list of the two keys with the highest values. Ties need to be broken alphabetically AZ with capital letters taking precedence over lowercase ones. Running the following sorts by the counts, but doesn't break the ties:

popular_fruits = sorted(fruits, key=fruits.get, reverse=True)
print(popular_fruits[0:2])

How can I accomplish this?

You can use something like this:

popular_fruits = sorted(fruits, key=lambda x: (-fruits[x], x))
print(popular_fruits[0:2])

EDIT:

A negation before fruits[x] is used to reverse the decreasing numeric ordering and in case of a tie the order is determined by the second argument of the tuple (alphabetically).

One cannot simply use sorted(fruits, key=lambda x: (fruits[x], x), reverse=True) because it will reverse ordering for both tuple elements, but we need to do it for the first element only.

You can do secondary sorting by using a tuple. I used -1 * the value to reverse it. "Capitals first" is the default sorting order for Python.

This is wrong even though it got some upvotes:

popular_fruits = sorted(fruits, key = lambda x: (-1 * x[1], x[0]))

# ['Apple', 'apple']

It should be as taras posted:

popular_fruits = sorted(fruits, key = lambda x: (-fruits[x], x))

I was sorting by the first and second letter and happened to give the first two items in order correctly but is actually completely wrong. I'll keep this up if people think it is useful to see an error that you can make by confusing a dict for a list of lists/tuples.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM