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 相关文章推荐
Pyramid添加Middleware的方法实例
Nov 27 Python
解析Python中的二进制位运算符
May 13 Python
简单谈谈python中的多进程
Nov 06 Python
python调用百度语音识别api
Aug 30 Python
Python3获取电脑IP、主机名、Mac地址的方法示例
Apr 11 Python
深入浅析python3中的unicode和bytes问题
Jul 03 Python
Python3.7 pyodbc完美配置访问access数据库
Oct 03 Python
python 实现在无序数组中找到中位数方法
Mar 03 Python
在python里使用await关键字来等另外一个协程的实例
May 04 Python
Python中qutip用法示例详解
Oct 02 Python
Python Pandas list列表数据列拆分成多行的方法实现
Dec 14 Python
Python批量解压&压缩文件夹的示例代码
Apr 04 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
Thinkphp实现MySQL读写分离操作示例
2014/06/25 PHP
yii实现使用CUploadedFile上传文件的方法
2015/12/28 PHP
PHP中使用foreach()遍历二维数组的简单实例
2016/06/13 PHP
PHP基于PDO调用sqlserver存储过程通用方法【基于Yii框架】
2017/10/07 PHP
Ubuntu上安装yaf扩展的方法
2018/01/29 PHP
jQuery 位置函数offset,innerWidth,innerHeight,outerWidth,outerHeight,scrollTop,scrollLeft
2010/03/23 Javascript
JQuery里选择超链接的实现代码
2011/05/22 Javascript
动态加载外部javascript文件的函数代码分享
2011/07/28 Javascript
extjs中form与grid交互数据(record)的方法
2013/08/29 Javascript
JS+DIV实现鼠标划过切换层效果的实例代码
2013/11/26 Javascript
js 操作select与option(示例讲解)
2013/12/20 Javascript
查询json的数据结构的8种方式简介
2014/03/10 Javascript
加载列表时jquery获取ul中第一个li的属性
2014/11/02 Javascript
学习Bootstrap滚动监听 附调用方法
2016/07/02 Javascript
js模式化窗口问题![window.dialogArguments]
2016/10/30 Javascript
网页挂马方式整理及详细介绍
2016/11/03 Javascript
AngularJS中的JSONP实例解析
2016/12/01 Javascript
Bootstrap风格的WPF样式
2016/12/07 Javascript
详解Vue 方法与事件处理器
2017/06/20 Javascript
使用js获取伪元素的content实例
2017/10/24 Javascript
JavaScript捕捉事件和阻止冒泡事件实例分析
2018/08/03 Javascript
通过js示例讲解时间复杂度与空间复杂度
2019/08/06 Javascript
解决layui弹框失效的问题
2019/09/09 Javascript
Bootstrap简单实用的表单验证插件BootstrapValidator用法实例详解
2020/03/29 Javascript
js实现鼠标切换图片(无定时器)
2021/01/27 Javascript
解决pycharm 安装numpy失败的问题
2019/12/05 Python
tensorflow dataset.shuffle、dataset.batch、dataset.repeat顺序区别详解
2020/06/03 Python
美国眼镜网:GlassesUSA
2017/09/07 全球购物
Lookfantastic瑞典:英国知名美妆购物网站
2018/04/06 全球购物
The North Face北面荷兰官网:美国著名户外品牌
2019/10/16 全球购物
PHP数据运算类型都有哪些
2013/11/05 面试题
小学生学雷锋演讲稿
2014/04/25 职场文书
设计顾问服务计划书
2014/05/04 职场文书
《索溪峪的野》教学反思
2016/02/19 职场文书
学生会自荐信
2019/05/16 职场文书
如何起草一份正确的合伙创业协议书?
2019/07/04 职场文书