python的sorted()函数的参数可以是列表、字典、元组、字符串,返回一个以列表为容器的返回值。
如果是字典将返回键的列表,相当于sorted(dict.keys());如果要得到字典的值排序列表,可以sorted(dict.values())。
sorted(dict.items())可以得到字典的键值对(按键排序)。
sorted(dict.items(),key=lambda i:i[1])可以得到字典按值排序的键值对序列。
sorted(iterable, /, *, key=None, reverse=False)
Return a new list containing all items from the iterable in ascending order.
A custom key function can be supplied to customize the sort order, and the
reverse flag can be set to request the result in descending order.
网友评论