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简单进程锁代码实例
Apr 27 Python
Python实现批量下载图片的方法
Jul 08 Python
python根据京东商品url获取产品价格
Aug 09 Python
Python使用迭代器捕获Generator返回值的方法
Apr 05 Python
python3+PyQt5实现支持多线程的页面索引器应用程序
Apr 20 Python
python实现画一颗树和一片森林
Jun 25 Python
在Python中给Nan值更改为0的方法
Oct 30 Python
基于树莓派的语音对话机器人
Jun 17 Python
python基于FTP实现文件传输相关功能代码实例
Sep 28 Python
python实现监控阿里云账户余额功能
Dec 16 Python
python3 xpath和requests应用详解
Mar 06 Python
屏蔽Django admin界面添加按钮的操作
Mar 11 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的类树(支持无限分类)
2006/10/09 PHP
php strtotime 函数UNIX时间戳
2009/01/14 PHP
php从完整文件路径中分离文件目录和文件名的方法
2015/03/13 PHP
PHP微信支付实例解析
2016/07/22 PHP
简单解决微信文章图片防盗链问题
2016/12/17 PHP
php实时倒计时功能实现方法详解
2017/02/27 PHP
LNMP部署laravel以及xhprof安装使用教程
2017/09/14 PHP
Django中通过定时任务触发页面静态化的处理方式
2018/08/29 PHP
PHP函数积累总结
2019/03/19 PHP
Laravel获取当前请求的控制器和方法以及中间件的例子
2019/10/11 PHP
基于PHP实现邮箱验证激活过程详解
2020/10/28 PHP
JavaScript DOM学习第六章 表单实例
2010/02/19 Javascript
location.href 在IE6中不跳转的解决方法与推荐使用代码
2010/07/08 Javascript
Jquery 例外被抛出且未被接住原因介绍
2013/09/04 Javascript
javascript 实现键盘上下左右功能的小例子
2013/09/15 Javascript
jquery让指定的元素闪烁显示的方法
2015/03/17 Javascript
JavaScript通过事件代理高亮显示表格行的方法
2015/05/27 Javascript
javascript中new关键字详解
2015/12/14 Javascript
基于vue实现圆形菜单栏组件
2019/07/05 Javascript
基于layPage插件实现两种分页方式浅析
2019/07/27 Javascript
Vue+Element UI+vue-quill-editor富文本编辑器及插入图片自定义
2019/08/20 Javascript
分析Python中设计模式之Decorator装饰器模式的要点
2016/03/02 Python
浅谈Python中重载isinstance继承关系的问题
2018/05/04 Python
如何基于Python创建目录文件夹
2019/12/31 Python
mac 上配置Pycharm连接远程服务器并实现使用远程服务器Python解释器的方法
2020/03/19 Python
浅谈tensorflow 中的图片读取和裁剪方式
2020/06/30 Python
Django数据库迁移常见使用方法
2020/11/12 Python
户外拓展活动方案
2014/02/11 职场文书
平面设计专业大学生职业规划书
2014/03/12 职场文书
会计师事务所实习证明
2014/11/16 职场文书
2014业务员年终工作总结
2014/12/09 职场文书
个人工作违纪检讨书
2015/05/05 职场文书
萤火虫之墓观后感
2015/06/05 职场文书
协议书格式模板
2016/03/24 职场文书
浅谈Python实现opencv之图片色素的数值运算和逻辑运算
2021/06/23 Python
Windows Server 2012配置DNS服务器的方法
2022/04/29 Servers