码迷,mamicode.com
首页 > 编程语言 > 详细

python 排序sorted

时间:2017-05-11 13:30:10      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:list   包括   href   ever   定义   int   div   参数   style   

num = [3,2,4,6,5]
anum = sorted(num)
dnum = sorted(num,reverse=True)
print 升序:,anum     # 升序: [2, 3, 4, 5, 6]
print 降序:,dnum     # 降序: [6, 5, 4, 3, 2]
help(sorted) # sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list

sorted()最多可以接收4个参数,iterable是可迭代对象,包括3类:

第一类是所有的序列类型,比如list(列表)、str(字符串)、tuple(元组)。
第二类是一些非序列类型,比如dict(字典)、file(文件)。
第三类是你定义的任何包含__iter__()或__getitem__()方法的类的对象。
 
 

python 排序sorted

标签:list   包括   href   ever   定义   int   div   参数   style   

原文地址:http://www.cnblogs.com/yiyezhouming/p/6840410.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!