Python3 Sorted() 用法

原创
2017/10/17 17:34
阅读数 109

Signature: sorted(iterable, /, *, key=None, reverse=False)

 

Docstring:

Return a new list containing all items from the iterable in ascending order.

  *返回一个排序后的list

 

A custom key function can be supplied to customize the sort order, and the

reverse flag can be set to request the result in descending order.

Type:      builtin_function_or_method

 

 

KEY参数:

 

key指定的函数将作用于list的每一个元素上,并根据key函数返回的结果进行排序。

对比原始的list和经过key=abs处理过的list

 

示例

s=‘123456’

sorted(s,key=lambda x:int(x))

# ['1', '2', '3', '4', '5', '6']

 

 

reverse参数:

 

reverse指定是排序方向,比如是按照key返回结果的大小排序, 默认是从小到大的

s=‘123456’

sorted(s,key=lambda x:int(x),reverse=True)

# ['6', '5', '4', '3', '2', '1']

 

 

 

 

 

 

 

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
0 评论
0 收藏
0
分享
返回顶部
顶部