Python内置函数sorted()可以对列表、元组、字典、集合、字符串、range对象以及其他可迭代对象进行排序,返回排序后的列表,支持使用key参数指定排序规则,支持reverse参数指定升序或者降序...3, 4, 5, 6, 7, 8, 9]
#降序排列
>>> sorted(range(10), reverse=True)
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
#对字符串中的字符升序排序...[' ', 'd', 'e', 'h', 'l', 'l', 'l', '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(d.items())
[('a', 3), ('b', 2), ('c', 5), ('d', 0)]