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

sorted(iterable,key=None,reverse=False),返回新的列表,对所有可迭代的对象均有效

sort(key=None,reverse=False) 就地改变列表  reverse:True反序;False 正序

Example1:

>>>sorted([1,5,3,2,9])

[1,2,3,5,9]

>>>a=[5,3,2,1,4]

>>>a.sort()

>>>a

[1,2,3,4,5]   #若用list.sort()则list本身将被修改

>>>sorted({1: 'D', 2: 'B', 3: 'B', 4: 'E', 5: 'A'})

[1,2,3,4,5]   #sorted()对所有的可迭代序列都有效

在python2.4开始,list.sort()和sorted()增加key参数来指定一个函数,此函数在每个元素比较前被调用。

Example2:

>>>sorted("This is a test string from Andrew".split(), key=str.lower)  #加了key,忽略大小写

['a', 'Andrew', 'from', 'is', 'string', 'test', 'This']                 #key=len按照长度进行排序

>>>sorted("This is a test string from Andrew".split())    #未加key,默认大写在前,小写在后

['Andrew', 'This', 'a', 'from', 'is', 'string', 'test']

更多的情况是用复杂对象的某些值来对复杂对象进行排序。

Example3:

>>> student_tuples = [('john', 'A', 15),('jane', 'B', 12),('dave', 'B', 10),]

>>> sorted(student_tuples, key=lambda student: student[2])   # sort by age

[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]

>>>student_tuples.sort(key=lambda x: x[2])

[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]

Example4:

>>>s=”Hello79351WorldMyNameIsMrFiona0352231964”

>>>''.join(sorted(s,key=lambda x: (x.isdigit(),x.isdigit() and int(x)%2==0,x.islower(),x.isupper(),x)))

'FHIMMNWaadeeilllmnooorrsy113335579902246'

大写在前,小写在后,数字放在最后并且奇数在偶数之前

>>>s={‘a’:10,’t’:5,’c’:2,’b’:12}

>>>sorted(s,key=lambda x:x[0])

[‘a’,’b’,’c’,’t’]

>>>s=[]

原文地址:https://www.cnblogs.com/MrFiona/p/5958918.html

python排序sorted与sort比较 (转)的更多相关文章

  1. python排序sorted与sort比较

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

  2. python排序 sorted()与list.sort() (转)

    该文章为转载:原文地址为:https://www.cnblogs.com/zuizui1204/p/6422939.html 只要是可迭代对象都可以用sorted . sorted(itrearble ...

  3. python 排序 sorted 如果第一个条件 相同 则按第二个条件排序

    怎样遍历一个list 符合下列条件 1. 按照元组的第一个从小到大排序 2. 如果第一个相同 则按照元组第2个从大到小 排序 a = [[2,3],[4,1],(2,8),(2,1),(3,4)] b ...

  4. python 排序sorted

    num = [3,2,4,6,5] anum = sorted(num) dnum = sorted(num,reverse=True) print '升序:',anum # 升序: [2, 3, 4 ...

  5. python 中 sorted() 和 list.sort() 的用法

    今天用python自带的sorted对一个列表进行排序, 在这里总结一下 只要是可迭代对象都可以用sorted . sorted(itrearble, cmp=None, key=None, reve ...

  6. 【转】python中List的sort方法(或者sorted内建函数)的用法

    原始出处:http://gaopenghigh.iteye.com/blog/1483864 python列表排序 简单记一下python中List的sort方法(或者sorted内建函数)的用法. ...

  7. 《算法4》2.1 - 选择排序算法(Selection Sort), Python实现

    选择排序算法(Selection Sort)是排序算法的一种初级算法.虽然比较简单,但是基础,理解了有助于后面学习更高深算法,勿以勿小而不为. 排序算法的语言描述: 给定一组物体,根据他们的某种可量化 ...

  8. python中sorted()和set()去重,排序

    前言 在看一个聊天机器人的神经网络模型训练前准备训练数据,需要对训练材料做处理(转化成张量)需要先提炼词干,然后对词干做去重和排序 words = sorted(list(set(words))) 对 ...

  9. python函数之sorted与sort

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

随机推荐

  1. Object Detection: To Be Higher Accuracy and Faster

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/51597496 在深度学习中有一类研究热 ...

  2. [POJ1456]Supermarket(贪心 + 优先队列 || 并查集)

    传送门 1.贪心 + 优先队列 按照时间排序从前往后 很简单不多说 ——代码 #include <queue> #include <cstdio> #include <i ...

  3. CodeForces - 205B - Little Elephant and Sorting

    先上题目: Little Elephant and Sorting time limit per test 1 second memory limit per test 256 megabytes i ...

  4. F - True Liars 带权并查集

    After having drifted about in a small boat for a couple of days, Akira Crusoe Maeda was finally cast ...

  5. [bzoj3339]Rmq Problem||[bzoj3585]mex_线段树

    Rmq Problem bzoj-3339||mex bzoj-3585 题目大意:给定一个长度为n的数列a,多次讯问区间l,r中最小的不属于集合{$A_l,A_{l+1}...A_r$}的非负整数. ...

  6. sysman 账号 oracle em console的一些问题

    前几天用下面的命令修改了 sysman的密码. [oracle@racnode1 ~]$ emctl setpasswd dbconsole Oracle Enterprise Manager 11g ...

  7. PMVS学习中学习c++

    最近忙于PMVS算法的优化,在这个过程中把这个写下来.仿照已有PMVS源程序,给出自己的一个实现过程. 1.fstream的使用 2.c++中的初始化 3.new创建对象与不用new的区别   我们学 ...

  8. iOS:让标题栏背景图片适应iOS7

    From google: If your app uses a custom image as the background of the bar, you'll need to provide a ...

  9. linux下的C语言开发(gdb调试)

    原文: http://blog.csdn.net/feixiaoxing/article/details/7199643 用gdb调试多进程的程序会遇到困难,gdb只能跟踪一个进程(默认是跟踪父进程) ...

  10. input子系统驱动学习之中的一个

        刚開始学习linux这门课就被分配编写一个设备的input子系统驱动.这对我的确有点困难.只是实际的操作中发现困难远比我想象的要大的多.本以为依照老师课上的步骤就行非常快的完毕这项任务.后来发 ...