sorted(iterable[, key][, reverse])
从 iterable 中的项目返回新的排序列表。
有两个可选参数,必须指定为关键字参数。
key 指定一个参数的函数,用于从每个列表元素中提取比较键:key=str.lower。默认值为 None (直接比较元素)。
reverse 是一个布尔值。如果设置为 True,那么列表元素将按照每个比较反转进行排序。
- You will be sorting the following list by each element’s second letter, a to z. Create a function to use when sorting, called second_let. It will take a string as input and return the second letter of that string. Then sort the list, create a variable called sorted_by_second_let and assign the sorted list to it. Do not use lambda.
ex_lst = ['hi', 'how are you', 'bye', 'apple', 'zebra', 'dance']
def second_let(strings):
return strings[1]
sorted_by_second_let = sorted(ex_lst

本文介绍了Python内置的排序函数sorted的用法,包括如何使用key参数根据元素的特定部分进行排序,以及如何通过reverse参数实现降序排序。通过实例展示了如何通过自定义函数和lambda表达式对字符串列表按第二个字母排序,以及对数字列表按最后一个数字降序排序。
最低0.47元/天 解锁文章

被折叠的 条评论
为什么被折叠?



