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中装饰器的一个妙用
Feb 08 Python
Python selenium如何设置等待时间
Sep 15 Python
Python 结巴分词实现关键词抽取分析
Oct 21 Python
python3 图片referer防盗链的实现方法
Mar 12 Python
解决Python安装后pip不能用的问题
Jun 12 Python
对python中数组的del,remove,pop区别详解
Nov 07 Python
Python3多目标赋值及共享引用注意事项
May 27 Python
python函数装饰器之带参数的函数和带参数的装饰器用法示例
Nov 06 Python
python实现人机猜拳小游戏
Feb 03 Python
TFRecord文件查看包含的所有Features代码
Feb 17 Python
django 前端页面如何实现显示前N条数据
Mar 16 Python
Python面向对象之成员相关知识总结
Jun 24 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
模拟SQLSERVER的两个函数:dateadd(),datediff()
2006/10/09 PHP
ADODB类使用
2006/11/25 PHP
php输出含有“#”字符串的方法
2017/01/18 PHP
Laravel给生产环境添加监听事件(SQL日志监听)
2017/06/19 PHP
CakePHP框架Model关联对象用法分析
2017/08/04 PHP
php记录搜索引擎爬行记录的实现代码
2018/03/02 PHP
laravel 错误处理,接口错误返回json代码
2019/10/25 PHP
Javascript String对象扩展HTML编码和解码的方法
2009/06/02 Javascript
如何使用Javascript获取距今n天前的日期
2013/07/08 Javascript
javascript强大的日期函数代码分享
2013/09/04 Javascript
JavaScript立即执行函数的三种不同写法
2014/09/05 Javascript
js图片模糊切换显示特效的方法
2015/02/17 Javascript
jQuery聚合函数实例
2015/05/21 Javascript
分享纯手写漂亮的表单验证
2015/11/19 Javascript
[原创]jQuery常用的4种加载方式分析
2016/07/25 Javascript
jQuery autoComplete插件两种使用方式及动态改变参数值的方法详解
2016/10/24 Javascript
解决vue项目中type=”file“ change事件只执行一次的问题
2018/05/16 Javascript
手把手教你vue-cli单页到多页应用的方法
2018/05/31 Javascript
Vue源码探究之状态初始化
2018/11/14 Javascript
详解如何使用router-link对象方式传递参数?
2019/05/02 Javascript
JavaScript实现打砖块游戏
2020/02/25 Javascript
js构造函数constructor和原型prototype原理与用法实例分析
2020/03/02 Javascript
JavaScript实现电灯开关小案例
2020/03/30 Javascript
[02:30]辉夜杯主赛事第二日胜者组半决赛 CDEC.Y赛后采访
2015/12/26 DOTA
简单介绍Python的Django框架的dj-scaffold项目
2015/05/30 Python
使用Python的Bottle框架写一个简单的服务接口的示例
2015/08/25 Python
详解Python中的__new__、__init__、__call__三个特殊方法
2016/06/02 Python
Python SVM(支持向量机)实现方法完整示例
2018/06/19 Python
python生成每日报表数据(Excel)并邮件发送的实例
2019/02/03 Python
python字典改变value值方法总结
2019/06/21 Python
python 爬虫基本使用——统计杭电oj题目正确率并排序
2020/10/26 Python
幼儿园义卖活动方案
2014/01/17 职场文书
幼儿园大班教学反思
2014/02/10 职场文书
小学班主任事迹材料
2014/12/17 职场文书
个人总结与自我评价2015
2015/03/11 职场文书
mysql字段为NULL索引是否会失效实例详解
2022/05/30 MySQL