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测试网络连通性示例【基于ping】
Aug 03 Python
Django 登陆验证码和中间件的实现
Aug 17 Python
Python读写zip压缩文件的方法
Aug 29 Python
python 用下标截取字符串的实例
Dec 25 Python
把JSON数据格式转换为Python的类对象方法详解(两种方法)
Jun 04 Python
Django使用中间键实现csrf认证详解
Jul 22 Python
详解Python 中sys.stdin.readline()的用法
Sep 12 Python
关于pandas的离散化,面元划分详解
Nov 22 Python
python的faker库用法
Nov 28 Python
python numpy实现多次循环读取文件 等间隔过滤数据示例
Mar 14 Python
python读取并查看npz/npy文件数据以及数据显示方法
Apr 14 Python
使用Python开发冰球小游戏
Apr 30 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中的生成XML文件的4种方法分享
2012/10/06 PHP
php存储过程调用实例代码
2013/02/03 PHP
PHP以指定字段为索引返回数据库所取的数据数组
2013/06/30 PHP
PHP中的output_buffering详细介绍
2014/09/27 PHP
PHP制作3D扇形统计图以及对图片进行缩放操作实例
2014/10/23 PHP
学习php开源项目的源码指南
2014/12/21 PHP
php自动更新版权信息显示的方法
2015/06/19 PHP
Docker 如何布置PHP开发环境
2016/06/21 PHP
Thinkphp连表查询及数据导出方法示例
2016/10/15 PHP
php制作基于xml的RSS订阅源功能示例
2017/02/08 PHP
PHP+MySQL实现在线测试答题实例
2020/01/02 PHP
javascript错误的认识不用关心内存管理
2012/12/15 Javascript
jquery中load方法的用法及注意事项说明
2014/02/22 Javascript
javascript使用appendChild追加节点实例
2015/01/12 Javascript
JavaScript获取伪元素(Pseudo-Element)属性的方法技巧
2015/03/13 Javascript
设置点击文本框或图片弹出日历控件的实现代码
2016/05/12 Javascript
纯JS打造网页中checkbox和radio的美化效果
2016/10/13 Javascript
jQuery的$.extend 浅拷贝与深拷贝
2017/03/08 Javascript
基于HTML5+JS实现本地图片裁剪并上传功能
2017/03/24 Javascript
Vue2.0 axios前后端登陆拦截器(实例讲解)
2017/10/27 Javascript
vue打包之后生成一个配置文件修改接口的方法
2018/12/09 Javascript
深入理解Vue.js轻量高效的前端组件化方案
2018/12/10 Javascript
vue-router实现嵌套路由的讲解
2019/01/19 Javascript
JS使用对象的defineProperty进行变量监控操作示例
2019/02/02 Javascript
koa2 数据api中间件设计模型的实现方法
2020/07/13 Javascript
微信小程序自定义底部弹出框动画
2020/11/18 Javascript
Vue3+elementui plus创建项目的方法
2020/12/01 Vue.js
vue祖孙组件之间的数据传递案例
2020/12/07 Vue.js
pyqt5 使用cv2 显示图片,摄像头的实例
2019/06/27 Python
python多项式拟合之np.polyfit 和 np.polyld详解
2020/02/18 Python
在echarts中图例legend和坐标系grid实现左右布局实例
2020/05/16 Python
美国户外运动商店:Sun & Ski
2018/08/23 全球购物
戴森香港官方网站:Dyson香港
2021/02/11 全球购物
挂职锻炼个人总结
2015/03/05 职场文书
我的兄弟姐妹观后感
2015/06/15 职场文书
Vue监视数据的原理详解
2022/02/24 Vue.js