sorted
假设我们用一组tuple表示学生名字和成绩:
L = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)]
请用sorted()对上述列表分别按名字排序:
# -*- coding: utf-8 -*- L = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)] def by_name(t): return t[0] L2 = sorted(L, key=by_name) print(L2)
Run
[('Adam', 92), ('Bart', 66), ('Bob', 75), ('Lisa', 88)]
再按成绩从高到低排序:
# -*- coding: utf-8 -*- L = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)] def by_score(t): return -t[1] L2 = sorted(L, key=by_score) print(L2)
Run
[('Adam', 92), ('Lisa', 88), ('Bob', 75), ('Bart', 66)]
练习: L1 = {'Bob':75, 'Adam':92,'Bart':66,'Lisa':88} L2 = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)] print(L1['Bob']) print(L2[0][0])