首页 > 编程 > Python > 正文

Python实现字典按key或者value进行排序操作示例【sorted】

2019-11-25 12:56:46
字体:
来源:转载
供稿:网友

本文实例讲述了Python实现字典按key或者value进行排序操作。分享给大家供大家参考,具体如下:

要点:使用到了python的内建函数与lambda函数

代码如下:(可直接复制运行)

# -*- coding:utf-8 -*-#! python2print '------定义一个字典d1---------------------------------------'d1 = {'a':14, 'c':12, 'b':11, 'e':13, 'f':16, 'd':15}print '------打印d1---------------------------------------'print d1print '------遍历字典d1---------------------------------------'for i in d1:  print iprint '------遍历字典d1---------------------------------------'for temp in d1.items():  print tempprint '------遍历字典的key---------------------------------------'for key,value in d1.items():  print keyprint '------遍历字典的value---------------------------------------'for key,value in d1.items():  print valueprint '------遍历字典的key和value---------------------------------------'for key,value in d1.items():  print key,valueprint '---------------------------------------------'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 = ',res3print '------d1按value排序(倒序:从大到小)---------------------------------------'res4 = sorted(d1.iteritems(), key=lambda d:d[1], reverse=True)print 'res4 = ',res4print '------d1按key排序(倒序:从大到小)---------------------------------------'res5 = sorted(d1.iteritems(), key=lambda d:d[0], reverse=True)print 'res5 = ',res5print '------d1按key排序(正序:从小到大)---------------------------------------'res6 = sorted(d1.iteritems(), key=lambda d:d[0], reverse=False)print 'res6 = ',res6print '------d1中取出key排序后生成一个列表---------------------------------------'res7 = [key for key,value in res6] # 注:res6是d1按key排序(正序:从小到大)的结果print 'res7 = ',res7print '------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]

PS:这里再为大家推荐一款关于排序的演示工具供大家参考:

在线动画演示插入/选择/冒泡/归并/希尔/快速排序算法过程工具:
http://tools.VeVB.COm/aideddesign/paixu_ys

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数据结构与算法教程》、《Python列表(list)操作技巧总结》、《Python编码操作技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程

希望本文所述对大家Python程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表