Python实现字典按key或者value进行排序操作示例【sorted】


Posted in Python onMay 03, 2019

本文实例讲述了Python实现字典按key或者value进行排序操作。分享给大家供大家参考,具体如下:

要点:使用到了python的内建函数与lambda函数

代码如下:(可直接复制运行)

# -*- coding:utf-8 -*-
#! python2
print '------定义一个字典d1---------------------------------------'
d1 = {'a':14, 'c':12, 'b':11, 'e':13, 'f':16, 'd':15}
print '------打印d1---------------------------------------'
print d1
print '------遍历字典d1---------------------------------------'
for i in d1:
  print i
print '------遍历字典d1---------------------------------------'
for temp in d1.items():
  print temp
print '------遍历字典的key---------------------------------------'
for key,value in d1.items():
  print key
print '------遍历字典的value---------------------------------------'
for key,value in d1.items():
  print value
print '------遍历字典的key和value---------------------------------------'
for key,value in d1.items():
  print key,value
print '---------------------------------------------'
print '---------------------------------------------'
#
print '------d1.items()与其类型展示---------------------------------------'
res = d1.items()
print 'res = ',res, '\nres type is',type(res)
print '------d1.iteritems()与其类型展示---------------------------------------'
res2 = d1.iteritems()
print 'res = ',res2, '\nres2 type is',type(res2)
print '------d1按value排序(正序:从小到大)---------------------------------------'
res3 = sorted(d1.iteritems(), key=lambda d:d[1], reverse=False)
print 'res3 = ',res3
print '------d1按value排序(倒序:从大到小)---------------------------------------'
res4 = sorted(d1.iteritems(), key=lambda d:d[1], reverse=True)
print 'res4 = ',res4
print '------d1按key排序(倒序:从大到小)---------------------------------------'
res5 = sorted(d1.iteritems(), key=lambda d:d[0], reverse=True)
print 'res5 = ',res5
print '------d1按key排序(正序:从小到大)---------------------------------------'
res6 = sorted(d1.iteritems(), key=lambda d:d[0], reverse=False)
print 'res6 = ',res6
print '------d1中取出key排序后生成一个列表---------------------------------------'
res7 = [key for key,value in res6] # 注:res6是d1按key排序(正序:从小到大)的结果
print 'res7 = ',res7
print '------d1中取出value排序后生成一个列表---------------------------------------'
res8= [value for key,value in res3] # 注:res3是d1按value排序(正序:从小到大)的结果
print 'res8 = ',res8

运行结果:

------定义一个字典d1---------------------------------------
------打印d1---------------------------------------
{'a': 14, 'c': 12, 'b': 11, 'e': 13, 'd': 15, 'f': 16}
------遍历字典d1---------------------------------------
a
c
b
e
d
f
------遍历字典d1---------------------------------------
('a', 14)
('c', 12)
('b', 11)
('e', 13)
('d', 15)
('f', 16)
------遍历字典的key---------------------------------------
a
c
b
e
d
f
------遍历字典的value---------------------------------------
14
12
11
13
15
16
------遍历字典的key和value---------------------------------------
a 14
c 12
b 11
e 13
d 15
f 16
---------------------------------------------
---------------------------------------------
------d1.items()与其类型展示---------------------------------------
res =  [('a', 14), ('c', 12), ('b', 11), ('e', 13), ('d', 15), ('f', 16)]
res type is <type 'list'>
------d1.iteritems()与其类型展示---------------------------------------
res =  <dictionary-itemiterator object at 0x01271E40>
res2 type is <type 'dictionary-itemiterator'>
------d1按value排序(正序:从小到大)---------------------------------------
res3 =  [('b', 11), ('c', 12), ('e', 13), ('a', 14), ('d', 15), ('f', 16)]
------d1按value排序(倒序:从大到小)---------------------------------------
res4 =  [('f', 16), ('d', 15), ('a', 14), ('e', 13), ('c', 12), ('b', 11)]
------d1按key排序(倒序:从大到小)---------------------------------------
res5 =  [('f', 16), ('e', 13), ('d', 15), ('c', 12), ('b', 11), ('a', 14)]
------d1按key排序(正序:从小到大)---------------------------------------
res6 =  [('a', 14), ('b', 11), ('c', 12), ('d', 15), ('e', 13), ('f', 16)]
------d1中取出key排序后生成一个列表---------------------------------------
res7 =  ['a', 'b', 'c', 'd', 'e', 'f']
------d1中取出value排序后生成一个列表---------------------------------------
res8 =  [11, 12, 13, 14, 15, 16]

Python 相关文章推荐
Python中time模块和datetime模块的用法示例
Feb 28 Python
Python中int()函数的用法浅析
Oct 17 Python
Python 实现淘宝秒杀的示例代码
Jan 02 Python
Python cookbook(数据结构与算法)字典相关计算问题示例
Feb 18 Python
Python网络编程使用select实现socket全双工异步通信功能示例
Apr 09 Python
Python2比较当前图片跟图库哪个图片相似的方法示例
Sep 28 Python
如何基于Python制作有道翻译小工具
Dec 16 Python
python序列化与数据持久化实例详解
Dec 20 Python
python爬虫爬取监控教务系统的思路详解
Jan 08 Python
python+opencv实现移动侦测(帧差法)
Mar 20 Python
MxNet预训练模型到Pytorch模型的转换方式
May 25 Python
Python实现迪杰斯特拉算法并生成最短路径的示例代码
Dec 01 Python
Python3模拟curl发送post请求操作示例
May 03 #Python
零基础使用Python读写处理Excel表格的方法
May 02 #Python
Python TestCase中的断言方法介绍
May 02 #Python
Python3中的bytes和str类型详解
May 02 #Python
利用pyinstaller打包exe文件的基本教程
May 02 #Python
Python中psutil的介绍与用法
May 02 #Python
Python3.5字符串常用操作实例详解
May 01 #Python
You might like
phpMyAdmin 安装配置方法和问题解决
2009/06/08 PHP
提示Trying to clone an uncloneable object of class Imagic的解决
2011/10/27 PHP
PHP中替换键名的简易方法示例详解
2014/01/07 PHP
YII中assets的使用示例
2014/07/31 PHP
PHP实现的curl批量请求操作示例
2018/06/06 PHP
PHP获取当前系统时间的方法小结
2018/10/03 PHP
YII框架关联查询操作示例
2019/04/29 PHP
设定php简写功能的方法
2019/11/28 PHP
JavaScript DOM 学习第三章 内容表格
2010/02/19 Javascript
javascript与webservice的通信实现代码
2010/12/25 Javascript
IE6下focus与blur错乱的解决方案
2011/07/31 Javascript
在JavaScript中typeof的用途介绍
2013/04/11 Javascript
JavaScript调用ajax获取文本文件内容实现代码
2014/03/28 Javascript
Angularjs 基础入门
2014/12/26 Javascript
javascript实现无限级select联动菜单
2015/01/02 Javascript
JavaScript实现动态添加,删除行的方法实例详解
2015/07/02 Javascript
js querySelector() 使用方法
2016/12/21 Javascript
ng2学习笔记之bootstrap中的component使用教程
2017/03/09 Javascript
详解微信小程序设置底部导航栏目方法
2017/06/29 Javascript
bootstrap-table组合表头的实现方法
2017/09/07 Javascript
使用ef6创建oracle数据库的实体模型遇到的问题及解决方案
2017/11/09 Javascript
使用webpack搭建react开发环境的方法
2018/05/15 Javascript
Vue实现侧边菜单栏手风琴效果实例代码
2018/05/31 Javascript
深入理解 TypeScript Reflect Metadata
2019/12/12 Javascript
精读《Vue3.0 Function API》
2020/05/20 Javascript
Python数组条件过滤filter函数使用示例
2014/07/22 Python
Python学习笔记之os模块使用总结
2014/11/03 Python
Python的地形三维可视化Matplotlib和gdal使用实例
2017/12/09 Python
python3射线法判断点是否在多边形内
2019/06/28 Python
Python接口测试文件上传实例解析
2020/05/22 Python
澳大利亚免息网上购物:Shop Zero
2016/09/17 全球购物
ECCO爱步加拿大官网:北欧丹麦鞋履及皮具品牌
2017/07/08 全球购物
乐观大学生的自我评价
2014/01/10 职场文书
Java实现多文件上传功能
2021/06/30 Java/Android
vue实现可以快进后退的跑马灯组件
2022/04/08 Vue.js
python双向链表实例详解
2022/05/25 Python