python 排序sorted

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() 函数用法 

posted on 2017-05-11 12:45  一叶舟鸣  阅读(213)  评论(0编辑  收藏  举报

导航