python中的排序
来源:互联网 发布:手机相片制作软件 编辑:程序博客网 时间:2024/06/04 20:07
#coding:utf-8from operator import itemgettera = [1,4,3,2,5]b = [[5,2,3],[6,0,1],[1,3,2]]print sorted(a)#默认为升序排序print sorted(a,reverse=True)#reverse为True时,降序排列print sorted(b)#面对多维数组时,默认对第0个元素进行比较排序print sorted(b, key=itemgetter(1))print sorted(b, key=itemgetter(2), reverse=True)#面对多维数组的第1个元素,进行比较排序。但是需要import operatorprint sorted(b, key=lambda x:x[1])print sorted(b, key=lambda x:x[2], reverse=True)#对多维数组的排序也可以使用上面的方法,无需import operator
输出结果为:
[1, 2, 3, 4, 5]
[5, 4, 3, 2, 1]
[[1, 3, 2], [5, 2, 3], [6, 0, 1]]
[[6, 0, 1], [5, 2, 3], [1, 3, 2]]
[[5, 2, 3], [1, 3, 2], [6, 0, 1]]
[[6, 0, 1], [5, 2, 3], [1, 3, 2]]
[[5, 2, 3], [1, 3, 2], [6, 0, 1]]
0 0
- Python中的排序
- python中的归并排序
- Python中的排序
- python中的字典排序!
- python 字典中的排序
- python中的排序
- Python中的字典排序
- Python 中的排序
- python中的排序
- python 中的排序大法
- Python中的排序
- python中的排序
- python中的排序和numpy中的排序
- python中的列表排序操作
- Python中的对象的排序
- Python 中的列表排序函数
- 【Python】Python_learning2:python中的冒泡排序
- Python(8) python中的冒泡排序、快速排序
- BaseAdapter的三重境界,逗比式,普通式,文艺式
- 在多人同时登陆服务器的情况下单个用户被踢掉其桌面程序是否会关闭的问题
- 【图】犯罪团伙
- Android 同一个Service已经启动再多调用startService
- A gentle introduction to Tao.OpenGl using SimpleOpenGlControl.
- python中的排序
- POJ 1755 Triathlon I
- linux下安裝sougou拼音
- WebGL笔记 第二章 着色器
- CodeForces 126B Password 【kmp】
- 笔记
- MATLAB排列组合函数--nchoosek
- 对于安装在本机上的根证书的进一步了解
- jquery复选 单选 选中