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

本文实例讲述了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

------d1.iteritems()与其类型展示---------------------------------------

res =

res2 type is

------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:这里再为大家推荐一款关于排序的演示工具供大家参考:

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

你可能感兴趣的:(python字典按key排序)