sorted排序

排序函数sorted原型如下,接受一个可迭代的序列,可以自定义排序函数放在key中,可以对待排序序列先期进行处理,还可以指定正反序

sorted(iterable, /, *, key=None, reverse=False)
Return a new list containing all items from the iterable in ascendi

A custom key function can be supplied to customize the sort order,
reverse flag can be set to request the result in descending order.

作业:

L = [('Bob', 75), ('adam', 92), ('Bart', 66), ('Lisa', 88)]
def by_name(t):
    return str.lower(t[0])

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

  

原文地址:https://www.cnblogs.com/vonkimi/p/6896470.html