首页 > 编程语言 > 详细

Python 中的列表排序函数

时间:2016-04-01 06:42:48      阅读:629      评论:0      收藏:0      [点我收藏+]

sorted(iterable, cmp=None, key=None, reverse=False)

L.sort(cmp=None, key=None, reverse=False)

iterable:是可迭代类型;
cmp:用于比较的函数,比较什么由key决定,有默认值,迭代集合中的一项;
key:用列表元素的某个属性和函数进行作为关键字,有默认值,迭代集合中的一项;
reverse:排序规则. reverse = True 或者 reverse = False,有默认值。
返回值:是一个经过排序的可迭代类型,与iterable一样。


sort()与sorted()的不同在于,sort是在原位重新排列列表,而sorted()是产生一个新的列表。


samples:

L = [2, 8, 3, 50]
L.sort()
print(L)           # [2, 3, 8, 50]

L = [2, 8, 3, 50]
print(sorted(L))   # [2, 3, 8, 50]



本文出自 “火蓝” 博客,请务必保留此出处http://fireblue.blog.51cto.com/9934670/1758997

Python 中的列表排序函数

原文:http://fireblue.blog.51cto.com/9934670/1758997

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!