nums = [5, 2, 3, 1, 4]
# 基本排序
sorted(nums)
# 返回: [1, 2, 3, 4, 5]
# 字典排序
nums = {1: 'D', 2: 'B', 3: 'B', 4: 'E', 5: 'A'}
sorted(nums)
# 返回 [1, 2, 3, 4, 5]
sorted(nums.values())
# 返回: ['A', 'B', 'B', 'D', 'E']
注:和 nums.sort 区别是前者会对原列表进行修改
对列表、字典和元祖进行排序
from operator import itemgetter, attrgetter
student_tuples = [
('john', 'A', 15),
('jane', 'B', 12),
('dave', 'B', 10),
]
sorted(student_tuples, key=itemgetter(2))
# 返回: [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]