Python内置函数sorted()可以对列表、元组、字典、集合、字符串、range对象以及其他可迭代对象进行排序,返回排序后的列表,支持使用key参数指定排序规则,支持reverse参数指定升序或者降序...'o', 'o', 'r', 'w']
>>> d = {'a':3, 'b':2, 'c':5, 'd':0}
#默认对字典中的键进行排序
>>> sorted(d)
['a', 'b', 'c',...'d']
>>> sorted(d.keys())
['a', 'b', 'c', 'd']
#对字典中的值进行排序
>>> sorted(d.values())
[0, 2, 3, 5]
#对字典中的元素进行排序...']
#按字符串长度降序排序
>>> sorted(x, key=len, reverse=True)
['aba', 'aec', 'da', 'cc']
#按字符串长度降序排序
#注意负号的用法仅适用于数字...0的元素升序、下标为1的元素降序进行排序
>>> for item in sorted(x, key=lambda i: (i[0], -i[1])):
print(item)
[3, 2, 9,