a={'Mike':90,'Mark':91,'Amy':89} sorted_scores = sorted(a.items(), key=lambda x: (x[0], -x[1])) print(sorted_scores)
时间: 2024-05-29 19:10:36 浏览: 82
The output of the code will be:
[('Amy', 89), ('Mark', 91), ('Mike', 90)]
Explanation:
- The dictionary 'a' contains three key-value pairs representing the scores of three students.
- The 'sorted' function is used to sort the dictionary by the keys and the values.
- The 'items' method is used to convert the dictionary into a list of tuples where each tuple contains the key-value pair.
- The 'key' argument of the 'sorted' function is set to a lambda function that takes a tuple as input and returns a tuple of two values. The first value is the key itself (i.e., the name of the student), and the second value is the negative of the score. The negative is used to sort the scores in descending order.
- The resulting list of tuples is stored in the 'sorted_scores' variable and printed to the console.
阅读全文