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迭代器的使用方法实例
Nov 21 Python
使用python BeautifulSoup库抓取58手机维修信息
Nov 21 Python
Python过滤函数filter()使用自定义函数过滤序列实例
Aug 26 Python
Python创建xml文件示例
Mar 22 Python
python3.x 将byte转成字符串的方法
Jul 17 Python
windows下python安装小白入门教程
Sep 18 Python
Python这样操作能存储100多万行的xlsx文件
Apr 16 Python
python opencv调用笔记本摄像头
Aug 28 Python
Python如何实现机器人聊天
Sep 10 Python
python使用matplotlib绘制折线图的示例代码
Sep 22 Python
python更新数据库中某个字段的数据(方法详解)
Nov 18 Python
浅谈python中的多态
Jun 15 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
PHP-MySQL教程归纳总结
2008/06/07 PHP
php实现批量删除挂马文件及批量替换页面内容完整实例
2016/07/08 PHP
Laravel 队列使用的实现
2019/01/08 PHP
基于jquery的文本框与autocomplete结合使用(asp.net+json)
2012/05/30 Javascript
javascript中获取下个月一号,是星期几
2012/06/01 Javascript
JS性能优化笔记搜索整理
2013/08/21 Javascript
实例讲解JS中setTimeout()的用法
2016/01/28 Javascript
基于easyui checkbox 的一些操作处理方法
2017/07/10 Javascript
JavaScript常见JSON操作实例分析
2018/08/08 Javascript
Vue 实现列表动态添加和删除的两种方法小结
2018/09/07 Javascript
Angularjs之ngModel中的值验证绑定方法
2018/09/13 Javascript
微信小程序实现多选框全选与取消全选功能示例
2019/05/14 Javascript
解决layui动态加载复选框无法选中的问题
2019/09/20 Javascript
ant design中upload组件上传大文件,显示进度条进度的实例
2020/10/29 Javascript
在vant中使用时间选择器和popup弹出层的操作
2020/11/04 Javascript
Python作用域用法实例详解
2016/03/15 Python
Python之日期与时间处理模块(date和datetime)
2017/02/16 Python
python matplotlib画图实例代码分享
2017/12/27 Python
Python cookbook(字符串与文本)针对任意多的分隔符拆分字符串操作示例
2018/04/19 Python
PyQt5 QSerialPort子线程操作的实现
2018/04/21 Python
对python中if语句的真假判断实例详解
2019/02/18 Python
在python中实现同行输入/接收多个数据的示例
2019/07/20 Python
matlab中imadjust函数的作用及应用举例
2020/02/27 Python
使用html5 canvas 画时钟代码实例分享
2015/11/11 HTML / CSS
HTML5 新表单类型示例代码
2018/03/20 HTML / CSS
潘婷洗发水广告词
2014/03/14 职场文书
2014两会学习心得:榜样精神伴我行
2014/03/17 职场文书
班主任班级寄语大全
2014/04/04 职场文书
中秋寄语大全
2014/04/11 职场文书
反邪教标语
2014/06/23 职场文书
实习工作表现评语
2014/12/31 职场文书
音乐教师个人工作总结
2015/02/06 职场文书
消防演习通知
2015/04/25 职场文书
创业计划书之游泳馆
2019/09/16 职场文书
导游词之南京夫子庙
2019/12/09 职场文书
React Native项目框架搭建的一些心得体会
2021/05/28 Javascript