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改变日志(logging)存放位置的示例
Mar 27 Python
python中stdout输出不缓存的设置方法
May 29 Python
python编写网页爬虫脚本并实现APScheduler调度
Jul 28 Python
python中正则的使用指南
Dec 04 Python
解决python使用open打开文件中文乱码的问题
Dec 29 Python
Python2.7.10以上pip更新及其他包的安装教程
Jun 12 Python
浅述python2与python3的简单区别
Sep 19 Python
在Python中实现shuffle给列表洗牌
Nov 08 Python
python中如何使用分步式进程计算详解
Mar 22 Python
python实现一次性封装多条sql语句(begin end)
Jun 06 Python
详解Python 最短匹配模式
Jul 29 Python
Python如何对齐字符串
Jul 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 第二节 数据类型之字符串类型
2012/04/28 PHP
php日期转时间戳,指定日期转换成时间戳
2012/07/17 PHP
基于preg_match_all采集后数据处理的一点心得笔记(编码转换和正则匹配)
2014/01/31 PHP
php目录操作实例代码
2014/02/21 PHP
php语言的7种基本的排序方法
2020/12/28 PHP
PHP序列化操作方法分析
2016/09/28 PHP
yii框架结合charjs统计上一年与当前年数据的方法示例
2020/04/04 PHP
Jquery带搜索框的下拉菜单
2013/05/06 Javascript
javascript模拟地球旋转效果代码实例
2013/12/02 Javascript
jQuery实现的购物车物品数量加减功能代码
2016/11/16 Javascript
bootstrap网格系统使用方法解析
2017/01/13 Javascript
angularjs中的$eval方法详解
2017/04/24 Javascript
详解Node项目部署到云服务器上
2017/07/12 Javascript
Vue监听事件实现计数点击依次增加的方法
2018/09/26 Javascript
Vue.js 时间转换代码及时间戳转时间字符串
2018/10/16 Javascript
bootstrap table合并行数据并居中对齐效果
2018/10/17 Javascript
JavaScript实现与使用发布/订阅模式详解
2019/01/19 Javascript
vue+Element实现搜索关键字高亮功能
2019/05/28 Javascript
微信小程序mpvue点击按钮获取button值的方法
2019/05/29 Javascript
Vue CLI3中使用compass normalize的方法
2019/05/30 Javascript
详解在Vue.js编写更好的v-for循环的6种技巧
2020/04/14 Javascript
python爬虫入门教程--优雅的HTTP库requests(二)
2017/05/25 Python
解决Linux系统中python matplotlib画图的中文显示问题
2017/06/15 Python
Flask框架Flask-Login用法分析
2018/07/23 Python
pycharm new project变成灰色的解决方法
2019/06/27 Python
Tensorflow中tf.ConfigProto()的用法详解
2020/02/06 Python
PageFactory设计模式基于python实现
2020/04/14 Python
html5配合css3实现带提示文字的输入框(摆脱js)
2013/03/08 HTML / CSS
整个世界的设计师家具在哈恩:Designathome
2019/03/25 全球购物
初中女生自我鉴定
2013/12/19 职场文书
公积金转移接收函
2014/01/11 职场文书
毕业实习评语
2014/02/10 职场文书
电气工程自动化求职信
2014/03/14 职场文书
2015年数学教师工作总结
2015/05/20 职场文书
2015年行政执法工作总结
2015/05/23 职场文书
图解排序算法之希尔排序Java实现
2021/06/26 Java/Android