python dict排序之一
来源:互联网 发布:mysql 最近3天 编辑:程序博客网 时间:2024/05/21 07:47
python dict的特点就是无序的,按照key来提取相应value,如果我们需要对value进行排序的话,那python dict排序可以按照下面的方法来进行:
下面的是按照value的值从大到小的顺序来排序。
dic = {'a':31, 'bc':5, 'c':3, 'asd':4, 'aa':74, 'd':0}
dict= sorted(dic.iteritems(), key=lambda d:d[1], reverse = True)
print dict
输出的结果:
[('aa', 74), ('a', 31), ('bc', 5), ('asd', 4), ('c', 3), ('d', 0)]
下面我们分解下代码
print dic.iteritems() 得到[(键,值)]的列表。
然后用sorted方法,通过key这个参数,指定排序是按照value,也就是第一个元素d[1的值来排序。reverse = True表示是需要翻转的,默认是从小到大,翻转的话,那就是从大到小。
下面的是按照value的值从大到小的顺序来排序。
dic = {'a':31, 'bc':5, 'c':3, 'asd':4, 'aa':74, 'd':0}
dict= sorted(dic.iteritems(), key=lambda d:d[1], reverse = True)
print dict
输出的结果:
[('aa', 74), ('a', 31), ('bc', 5), ('asd', 4), ('c', 3), ('d', 0)]
下面我们分解下代码
print dic.iteritems() 得到[(键,值)]的列表。
然后用sorted方法,通过key这个参数,指定排序是按照value,也就是第一个元素d[1的值来排序。reverse = True表示是需要翻转的,默认是从小到大,翻转的话,那就是从大到小。
0 0
- python dict排序之一
- Python Dict 排序
- python dict sorted 排序
- python dict sorted 排序
- python dict sorted 排序
- python dict排序
- python dict sorted 排序
- Python dict词典排序
- python dict 排序
- python dict排序
- python dict按照value 排序
- python dict按照value 排序
- python dict list 排序专题
- python dict按照value 排序
- python dict 按key排序
- python dict按照value 排序
- python中dict和list排序
- python中dict和list排序
- VS2010MFC创建线程
- html5中关于canvas标签用法(绘图)
- (转)理解(Memory Reordering)从JVM并发看CPU内存指令重排序
- 事件处理
- 《轻松搞定网页设计》学习笔记系列之一
- python dict排序之一
- Head First Java_Study Notes_Chapter 1 Break the Surface
- 树莓派学习笔记——I2C设备载入和速率设置
- SDL游戏之路(八)--2D游戏添加3D元素
- python写的简答学生管理系统Demo(练习python语法)
- 五、java面向对象编程(二)——类变量、类方法
- JVM原理和优化
- 初识FreeRTOS
- 关于聚合的几个例子