python中sort和sorted总结
来源:互联网 发布:上瘾网络剧猛其其花絮 编辑:程序博客网 时间:2024/05/19 04:04
概要
list.sort(cmp=None, key=None, reverse=False):
list的一个成员函数,调用时list本身发生改变
- cmp: 排序函数
- key: 排序关键字
- reverse: 升序(False)或者降序(True)
sorted(iterable, cmp=None, key=None, reverse=False):
python的一个内部函数,对可迭代类型数据进行排序并返回,但不影响数据本身
- cmp: 排序函数
- key: 排序关键字
- reverse: 升序(False)或者降序(True)
简单排序
a1 = [3,2,1,6,5,4]a2 = [3,2,1,6,5,4]print a1 # [3, 2, 1, 6, 5, 4]a1.sort() #调用成员函数进行排序(默认为升序:reversr=False),返回值为‘none’print a1 # 重新输出a1,此时的a1已经改变:[1, 2, 3, 4, 5, 6]a1.sort(reverse=True) # 降序print a1 # [6, 5, 4, 3, 2, 1]print '\n'print a2 # [3, 2, 1, 6, 5, 4]print sorted(a2) # 对a2进行排序:[1, 2, 3, 4, 5, 6]print sorted(a2,reverse=True) #降序:[6, 5, 4, 3, 2, 1]print a2 # 此时a2不变:[3, 2, 1, 6, 5, 4]
[3, 2, 1, 6, 5, 4][1, 2, 3, 4, 5, 6][6, 5, 4, 3, 2, 1][3, 2, 1, 6, 5, 4][1, 2, 3, 4, 5, 6][6, 5, 4, 3, 2, 1][3, 2, 1, 6, 5, 4]
根据cmp函数进行排序
a1 = [['a',3],['c',2],['b',4],['d',1]]a2 = [['a',3],['c',2],['b',4],['d',1]]print a1a1.sort() # 默认根据每一个子列表中起始元素(索引为0)进行排序print a1a1.sort(cmp = lambda x,y:cmp(x[0],y[0])) # 根据每一个子列表中起始元素(索引为0)进行排序print a1a1.sort(cmp = lambda x,y:cmp(x[1],y[1])) # 根据每一个子列表中索引为1的元素进行排序print a1print '\n'## 用sorted函数对a2排序,不改变a2自身的值,其他与a2.sort()相同print a2print sorted(a2)print a2,'\n'print sorted(a2,cmp = lambda x,y:cmp(x[0],y[0]))print a2,'\n'print sorted(a2,cmp = lambda x,y:cmp(x[1],y[1]))print a2
[['a', 3], ['c', 2], ['b', 4], ['d', 1]][['a', 3], ['b', 4], ['c', 2], ['d', 1]][['a', 3], ['b', 4], ['c', 2], ['d', 1]][['d', 1], ['c', 2], ['a', 3], ['b', 4]][['a', 3], ['c', 2], ['b', 4], ['d', 1]][['a', 3], ['b', 4], ['c', 2], ['d', 1]][['a', 3], ['c', 2], ['b', 4], ['d', 1]] [['a', 3], ['b', 4], ['c', 2], ['d', 1]][['a', 3], ['c', 2], ['b', 4], ['d', 1]] [['d', 1], ['c', 2], ['a', 3], ['b', 4]][['a', 3], ['c', 2], ['b', 4], ['d', 1]]
根据key关键字进行排序
a1 = [['a',3],['c',2],['b',4],['d',1]]a2 = [['a',3],['c',2],['b',4],['d',1]]print a1a1.sort() # 默认根据每一个子列表中起始元素(索引为0)进行排序print a1a1.sort(key = lambda x:x[0]) # 根据每一个子列表中起始元素(索引为0)进行排序print a1a1.sort(key = lambda x:x[1]) # 根据每一个子列表中索引为1的元素进行排序print a1print '\n'## 用sorted函数对a2排序,不改变a2自身的值,其他与a2.sort()相同print a2print sorted(a2)print a2,'\n'print sorted(a2,key = lambda x:x[0])print a2,'\n'print sorted(a2,key = lambda x:x[1])print a2
[['a', 3], ['c', 2], ['b', 4], ['d', 1]][['a', 3], ['b', 4], ['c', 2], ['d', 1]][['a', 3], ['b', 4], ['c', 2], ['d', 1]][['d', 1], ['c', 2], ['a', 3], ['b', 4]][['a', 3], ['c', 2], ['b', 4], ['d', 1]][['a', 3], ['b', 4], ['c', 2], ['d', 1]][['a', 3], ['c', 2], ['b', 4], ['d', 1]] [['a', 3], ['b', 4], ['c', 2], ['d', 1]][['a', 3], ['c', 2], ['b', 4], ['d', 1]] [['d', 1], ['c', 2], ['a', 3], ['b', 4]][['a', 3], ['c', 2], ['b', 4], ['d', 1]]
阅读全文
0 0
- python中sort和sorted总结
- Python中sort 和 sorted函数
- python中sort和sorted函数小结
- python中sort和sorted函数
- Python中sort和sorted快排
- python sorted和sort
- 【python学习笔记】Python中排序sorted()和list.sort()
- python 中 sort(), sorted(), argsort()
- Python之 sort和sorted
- python有关sorted和sort
- python sort / sorted 和 argsort()
- python: sort和sorted()方法
- python中的 sort 和 sorted
- sort()和sorted函数总结
- Python中sort函数、sorted函数和argsort函数
- Python中排序sort 、sorted和argsort函数
- python 中 sorted() 和 list.sort() 的用法
- python 中 sorted() 和 list.sort() 的用法
- 将一个有序整数数组放在一个二叉树中
- CodeForces
- 比onload更快的DOMContentLoaded,readystatechange,doscroll的DOM加载事件
- Linux ssh自动登录的4种实现方法
- JavaScript入门(四)
- python中sort和sorted总结
- C++7.18
- git出现 *** Please tell me who you are. Run...... 错误
- CRC校验算法
- JVM内存区域划分(JDK6 7 8中的变化)
- css的三大特征
- C#编程入门_常用集合_14
- Error:warning: Ignoring InnerClasses attribute for an anonymous inner class
- 设计模式(7)--桥接模式