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实现将英文单词表示的数字转换成阿拉伯数字的方法
Jul 02 Python
python&amp;MongoDB爬取图书馆借阅记录
Feb 05 Python
python统计多维数组的行数和列数实例
Jun 23 Python
python读取几个G的csv文件方法
Jan 07 Python
对PyQt5中的菜单栏和工具栏实例详解
Jun 20 Python
Numpy之将矩阵拉成向量的实例
Nov 30 Python
python 解决cv2绘制中文乱码问题
Dec 23 Python
Python魔法方法 容器部方法详解
Jan 02 Python
Python中如何引入第三方模块
May 27 Python
关于Python3爬虫利器Appium的安装步骤
Jul 29 Python
实例讲解Python中sys.argv[]的用法
Jun 03 Python
python的netCDF4批量处理NC格式文件的操作方法
Mar 21 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
常用表单验证类,有了这个,一般的验证就都齐了。
2006/12/06 PHP
php常用的安全过滤函数集锦
2014/10/09 PHP
PHP receiveMail实现收邮件功能
2018/04/25 PHP
分享一个asp.net pager分页控件
2012/01/04 Javascript
javascript自定义右键弹出菜单实现方法
2015/05/25 Javascript
jQuery中prepend()方法使用详解
2015/08/11 Javascript
javascript中html字符串转化为jquery dom对象的方法
2015/08/27 Javascript
基于jquery实现图片上传本地预览功能
2016/01/08 Javascript
Nodejs学习item【入门手上】
2016/05/05 NodeJs
js中遍历对象的属性和值的方法
2016/07/27 Javascript
微信小程序 PHP后端form表单提交实例详解
2017/01/12 Javascript
微信小程序checkbox组件使用详解
2018/01/31 Javascript
vue 使用element-ui中的Notification自定义按钮并实现关闭功能及如何处理多个通知
2019/08/17 Javascript
在Layui 的表格模板中,实现layer父页面和子页面传值交互的方法
2019/09/10 Javascript
layui 解决富文本框form表单提交为空的问题
2019/10/26 Javascript
vue ssr+koa2构建服务端渲染的示例代码
2020/03/23 Javascript
javascript递归函数定义和用法示例分析
2020/07/22 Javascript
基于vuex实现购物车功能
2021/01/10 Vue.js
python网络编程之数据传输UDP实例分析
2015/05/20 Python
Python实现TCP协议下的端口映射功能的脚本程序示例
2016/06/14 Python
对python mayavi三维绘图的实现详解
2019/01/08 Python
Python3 安装PyQt5及exe打包图文教程
2019/01/08 Python
Django web框架使用url path name详解
2019/04/29 Python
python中的colorlog库使用详解
2019/07/05 Python
Python实现一个带权无回置随机抽选函数的方法
2019/07/24 Python
在pytorch中对非叶节点的变量计算梯度实例
2020/01/10 Python
Python 去除字符串中指定字符串
2020/03/05 Python
150行Python代码实现带界面的数独游戏
2020/04/04 Python
完美解决jupyter由于无法import新包的问题
2020/05/26 Python
南非最大的花卉和送礼服务:NetFlorist
2017/09/13 全球购物
土木工程专业大学毕业生求职信
2013/10/13 职场文书
英文自荐信格式
2013/11/28 职场文书
院领导写的就业推荐信
2014/03/09 职场文书
教师师德演讲稿
2014/05/06 职场文书
2014年小学语文工作总结
2014/12/20 职场文书
PyQt5爬取12306车票信息程序的实现
2021/05/14 Python