python排序sorted与sort比较 (转)
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比较 (转)的更多相关文章
- python排序sorted与sort比较
Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列. sorted(iterable,key=None,revers ...
- python排序 sorted()与list.sort() (转)
该文章为转载:原文地址为:https://www.cnblogs.com/zuizui1204/p/6422939.html 只要是可迭代对象都可以用sorted . sorted(itrearble ...
- python 排序 sorted 如果第一个条件 相同 则按第二个条件排序
怎样遍历一个list 符合下列条件 1. 按照元组的第一个从小到大排序 2. 如果第一个相同 则按照元组第2个从大到小 排序 a = [[2,3],[4,1],(2,8),(2,1),(3,4)] b ...
- python 排序sorted
num = [3,2,4,6,5] anum = sorted(num) dnum = sorted(num,reverse=True) print '升序:',anum # 升序: [2, 3, 4 ...
- python 中 sorted() 和 list.sort() 的用法
今天用python自带的sorted对一个列表进行排序, 在这里总结一下 只要是可迭代对象都可以用sorted . sorted(itrearble, cmp=None, key=None, reve ...
- 【转】python中List的sort方法(或者sorted内建函数)的用法
原始出处:http://gaopenghigh.iteye.com/blog/1483864 python列表排序 简单记一下python中List的sort方法(或者sorted内建函数)的用法. ...
- 《算法4》2.1 - 选择排序算法(Selection Sort), Python实现
选择排序算法(Selection Sort)是排序算法的一种初级算法.虽然比较简单,但是基础,理解了有助于后面学习更高深算法,勿以勿小而不为. 排序算法的语言描述: 给定一组物体,根据他们的某种可量化 ...
- python中sorted()和set()去重,排序
前言 在看一个聊天机器人的神经网络模型训练前准备训练数据,需要对训练材料做处理(转化成张量)需要先提炼词干,然后对词干做去重和排序 words = sorted(list(set(words))) 对 ...
- python函数之sorted与sort
Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列. sorted(iterable,key=None,revers ...
随机推荐
- [COGS 2264]魔法传输
[COGS 2264]魔法传输 题目 自从看了<哈利波特>,小Y就十分渴望获得魔法值.于是他和一群向往魔法的孩子(当然这些孩子们都是不会魔法的)来到了哈利波特的家,大家坐成一排.哈利波特会 ...
- Sencha Toucha 2.1 文件上传
javascript代码: Ext.onReady(function() { Ext.create('Ext.form.Panel', { title: 'Upload a Photo', width ...
- Square words(codevs 3301)
题目描述 Description 定义square words为: 1.长度为偶数. 2.前一半等于后一半. 比如abcabc和aaaa都是square words,但是abcabcab和aaaaa都 ...
- Spring MVC-集成(Integration)-Hibernate验证器示例(转载实践)
以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_hibernate_validator.htm 说明:示例基于Spring MVC ...
- [PWA] Show Notifications when a Service Worker is Installed or Updated
Service Workers get installed and activated in the background, but until we reload the page they don ...
- 为datatable添加自增列
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- Android 外部存储权限分析
不知道你有么有发现.来自菜鸟的成长史:http://blog.csdn.net/zjbpku/article/details/25161131. KitKat之后的版本号不再支持用户对外置SDcard ...
- [Spring] Spring Boot 生态
- word2vec改进之Hierarchical Softmax
首先Hierarchical Softmax是word2vec的一种改进方式,因为传统的word2vec需要巨大的计算量,所以该方法主要有两个改进点: 1. 对于从输入层到隐藏层的映射,没有采取神经网 ...
- Windows7下caffe-ssd-microsoft下编译
整个编译可谓漫长 编译了两天 网上教程也很多 但是也很杂 遇到各种错误 总归是编完了 1.下载Windows版本的Caffe-SSD源码 下载链接:https://github.com/conner9 ...