首页 > 其他 > 详细

sort 和sorted的 区别

时间:2018-08-06 21:29:17      阅读:156      评论:0      收藏:0      [点我收藏+]
sort(cmp=None, key=None, reverse=False)
sorted(iterable, cmp=None, key=None, reverse=False)
 
 
sort是容器的函数,用List的成员函数sort进行排序
sorted是Python的内建函数相同的参数,用built-in函数sorted进行排序
sorted(iterable,key=None,reverse=False),返回新的列表,对所有可迭代的对象均有效
sort(key=None,reverse=False) 就地改变列表 reverse:True反序;False 正序
1 a = [1,2,3,5,2,6,8,0]
2 print(a.sort())# None
3 print(a)# [0, 1, 2, 2, 3, 5, 6, 8]
4 a = [1,2,3,5,2,6,8,0]
5 print(sorted(a)) # [0, 1, 2, 2, 3, 5, 6, 8]
6 
7 print(a) # [1, 2, 3, 5, 2, 6, 8, 0]

 

sort 和sorted的 区别

原文:https://www.cnblogs.com/JerryZao/p/9433026.html

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