首页 > 其他 > 详细

sort与sorted

时间:2017-02-17 23:16:41      阅读:223      评论:0      收藏:0      [点我收藏+]

Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列。

 

1.list.sort()方法仅被定义在list中,相反地sorted()方法对所有的可迭代序列都有效。

2.使用方式:  使用sort只能list.sort(),不能sort(list)

        使用sorted只能sorted(iteritems),不能iteritems.sorted

3.sort要改变原来的list

 sorted返回一个新顺序的iteritems,但不会改变原来的iteritems

  

>>> a = [2,1,5]
>>> b = a.sort()    会发现这一步并没有对b进行赋值,在a排完序后在用b = a去赋值才成功
>>> print a
>>> [1,2,5]
>>> print b
>>> None
>>> b = a
>>> print b
>>> [1,2,5]



>>> a = [2,1,5]
>>> b = sorted(a)
>>> b
>>> [1,2,5]
>>> a 
>>> [1,2,5]

 

 

 

http://www.cnblogs.com/nju2014/p/5569983.html

 

sort与sorted

原文:http://www.cnblogs.com/ymjyqsx/p/6411782.html

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