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 <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.jb51.net/aideddesign/paixu_ys

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

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

(0)

相关推荐

  • python3.0 字典key排序

    IDLE 3.0 >>> dic = {"aa":1,"bb":2,"ab":3} >>> dic {'aa': 1, 'ab': 3, 'bb': 2} >>> for k in sorted(dic.keys()): print (k) aa ab ----------------------------------------------- 字典对象其实就是键-值对 下面是字典对象的添加

  • python字典多条件排序方法实例

    项目编写过程中,总能遇见对字典进行排序什么的,如果要实现多条件排序只需要下面几行代码实现.充分体现了python的好处了. 复制代码 代码如下: teamitems = [{'team':'France'     , 'P':1 , 'GD':-3 , 'GS':1 , 'GA':4},             {'team':'Uruguay'     , 'P':7 , 'GD':4  , 'GS':4 , 'GA':0},             {'team':'SouthAfrica'

  • Python使用sorted对字典的key或value排序

    sorted函数 sorted(iterable,key,reverse) iterable 待排序的可迭代对象 key 对应的是个函数, 该函数用来决定选取用哪些值来进行排序 reverse 反转排序 对key排序 d: dict = {"p": 59, "o": 9, "s": 5, "a": 20, "z": 18} li: list = sorted(d.keys()) print(li) 执行结果

  • Python实现字典依据value排序

    具体内容如下: 使用sorted将字典按照其value大小排序 >>> record = {'a':89, 'b':86, 'c':99, 'd':100} >>> sorted(record.items(), key=lambda x:x[1]) [('b', 86), ('a', 89), ('c', 99), ('d', 100)] sorted第一个参数要可迭代,可以为tuple, list >>> items = [(1, 'B'), (1,

  • 详解python的sorted函数对字典按key排序和按value排序

    1.sorted函数按key值对字典排序 先来基本介绍一下sorted函数,sorted(iterable,key,reverse),sorted一共有iterable,key,reverse这三个参数. 其中iterable表示可以迭代的对象,例如可以是 dict.items().dict.keys()等,key是一个函数,用来选取参与比较的元素,reverse则是用来指定排序是倒序还是顺 序,reverse=true则是倒序,reverse=false时则是顺序,默认时reverse=fal

  • python 字典(dict)按键和值排序

    python 字典(dict)的特点就是无序的,按照键(key)来提取相应值(value),如果我们需要字典按值排序的话,那可以用下面的方法来进行: 1 下面的是按照value的值从大到小的顺序来排序. dic = {'a':31, 'bc':5, 'c':3, 'asd':4, 'aa':74, 'd':0} dict= sorted(dic.items(), key=lambda d:d[1], reverse = True) print(dict) 输出的结果: [('aa', 74),

  • python字典值排序并取出前n个key值的方法

    今天在写一个算法的过程中,得到了一个类似下面的字典: {'user1':0.456,'user2':0.999,'user3':0.789,user:'0.234'} 想要获取字典里value值前3的key,就产生了如下代码 直接贴代码: def order_dict(dicts, n): result = [] result1 = [] p = sorted([(k, v) for k, v in dicts.items()], reverse=True) s = set() for i in

  • Python实现的字典排序操作示例【按键名key与键值value排序】

    本文实例讲述了Python实现的字典排序操作.分享给大家供大家参考,具体如下: 对字典进行排序?这其实是一个伪命题,搞清楚python字典的定义---字典本身默认以key的字符顺序输出显示---就像我们用的真实的字典一样,按照abcd字母的顺序排列,并且本质上各自没有先后关系,是一个哈希表的结构: 但实际应用中我们确实有这种排序的"需求"-----按照values的值"排序"输出,或者按照别的奇怪的顺序进行输出,我们只需要把字典转化成list或者tuple,把字典每

  • Python中字典(dict)和列表(list)的排序方法实例

    一.对列表(list)进行排序 推荐的排序方式是使用内建的sort()方法,速度最快而且属于稳定排序 复制代码 代码如下: >>> a = [1,9,3,7,2,0,5]>>> a.sort()>>> print a[0, 1, 2, 3, 5, 7, 9]>>> a.sort(reverse=True)>>> print a[9, 7, 5, 3, 2, 1, 0]>>> b = ['e','a'

  • Python实现对字典分别按键(key)和值(value)进行排序的方法分析

    本文实例讲述了Python实现对字典分别按键(key)和值(value)进行排序的方法.分享给大家供大家参考,具体如下: 方法一: #使用sorted函数进行排序 ''' sorted(iterable,key,reverse),sorted一共有iterable,key,reverse这三个参数; 其中iterable表示可以迭代的对象,例如可以是dict.items().dict.keys()等 key是一个函数,用来选取参与比较的元素,reverse则是用来指定排序是倒序还是顺序,reve

随机推荐