Discuss / Python / 练习题

练习题

Topic source

唯情恋昉

#1 Created at ... [Delete] [Delete and Lock User]

练习一

L = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)]
# t相当于是('Bob', 75)
def by_name(t):
    # t[0]相当于是'Bob'
    return t[0]

L2 = sorted(L, key=by_name)
print(L2)

练习二

L = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)]
# t相当于是('Bob', 75)
def by_score(t):
    # t[1]相当于是 75
    return t[1]

L2 = sorted(L, key=by_score)
print(L2)

  • 1

Reply