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正则匹配查询港澳通行证办理进度示例分享
Dec 27 Python
Python生成器(Generator)详解
Apr 13 Python
Python 函数基础知识汇总
Mar 09 Python
pytorch构建网络模型的4种方法
Apr 13 Python
解决Matplotlib图表不能在Pycharm中显示的问题
May 24 Python
python 函数内部修改外部变量的方法
Dec 18 Python
浅谈pandas筛选出表中满足另一个表所有条件的数据方法
Feb 08 Python
python识别文字(基于tesseract)代码实例
Aug 24 Python
详解Python中Pyyaml模块的使用
Oct 08 Python
python 实现图片批量压缩的示例
Dec 18 Python
Python列表元素删除和remove()方法详解
Jan 04 Python
python使用openpyxl库读写Excel表格的方法(增删改查操作)
May 02 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中socket通信机制实例详解
2015/01/03 PHP
WordPress 插件——CoolCode使用方法与下载
2007/07/02 Javascript
比较详细的关于javascript中void(0)的具体含义解释
2007/08/02 Javascript
用js实现的模拟jquery的animate自定义动画(2.5K)
2010/07/20 Javascript
NodeJS的url截取模块url-extract的使用实例
2013/11/18 NodeJs
用jQuery与JSONP轻松解决跨域访问的问题
2014/02/04 Javascript
jquery遍历checkbox介绍
2014/02/21 Javascript
JSON+HTML实现国家省市联动选择效果
2014/05/18 Javascript
JS使用ajax从xml文件动态获取数据显示的方法
2015/03/24 Javascript
在Javascript中处理字符串之big()方法的使用
2015/06/08 Javascript
JavaScript文本框脚本编写的注意事项
2016/01/25 Javascript
详解自动生成博客目录案例
2016/12/09 Javascript
微信小程序实现默认第一个选中变色效果
2018/07/17 Javascript
Vue.js 实现数据展示全部和收起功能
2018/09/05 Javascript
[57:22]2018DOTA2亚洲邀请赛 4.7总决赛 LGD vs Mineski 第五场
2018/04/10 DOTA
Python中的CURL PycURL使用例子
2014/06/01 Python
python 数字类型和字符串类型的相互转换实例
2018/07/17 Python
关于python列表增加元素的三种操作方法
2018/08/22 Python
详解Python3注释知识点
2019/02/19 Python
python实现手机销售管理系统
2019/03/19 Python
Python提取转移文件夹内所有.jpg文件并查看每一帧的方法
2019/06/27 Python
python开头的coding设置方法
2019/08/08 Python
pytorch SENet实现案例
2020/06/24 Python
世界领先的以旅馆为主的在线预订平台:Hostelworld
2016/10/09 全球购物
正宗的日本零食和糖果订阅盒:Bokksu
2019/11/21 全球购物
后勤部长岗位职责
2013/12/14 职场文书
医生进修自我鉴定
2014/01/19 职场文书
毕业生就业推荐表自我鉴定
2014/03/20 职场文书
《新型玻璃》教学反思
2014/04/13 职场文书
财务内勤岗位职责
2014/04/17 职场文书
秋天的怀念教学反思
2014/04/28 职场文书
我的未来不是梦演讲稿
2014/09/02 职场文书
2015年幼儿园安全工作总结
2015/05/12 职场文书
公司员工手册范本
2015/05/14 职场文书
Python如何使用logging为Flask增加logid
2021/03/30 Python
Mysql调整优化之四种分区方式以及组合分区
2022/04/13 MySQL