python中的dict是不能排序的,只有对dict的representation进行排序,例如list或者tuple

排序肯定会用到sorted函数,那么我们就来讲一下sorted函数。

sorted

  • sorted(iterable,key,reverse)

iterable:表示可迭代的对象,例如dict.keys(), dict.items()

key:是一个函数,用来选择参与排序的元素

reverse:默认为False,从小到大排序,reverse=True,表示从大到小排序

按 key 排序

import operator
x = {1:2, 5:6, 3:4}
sorted_x = sorted(x.items(), key=operator.itemgetter(0))
# sorted_x = sorted(x.items(), key=operator.itemgetter(0), reverse=True)

itemgetter(0)表示取每项的第一个值,也就是键,来参与排序,默认是从小到大排序

结果 [(1, 2), (3, 4), (5, 6)]

按 value 排序

import operator
x = {1:2, 5:6, 3:4}
sorted_x = sorted(x.items(), key=operator.itemgetter(1))

itemgetter(1)表示取每项的值来排序

结果 [(1, 2), (3, 4), (5, 6)]

按 key 排序

def sortedDictValues1(adict):
items = adict.items()
items.sort()
return [value for key, value in items]
def sortedDictValues2(adict):
keys = adict.keys()
keys.sort()
return [dict[key] for key in keys]
def sortedDictValues3(adict):
keys = adict.keys()
keys.sort()
return map(adict.get, keys)
#一行语句搞定:
[(k,di[k]) for k in sorted(di.keys())]

按 value 排序

#还是一行搞定:
[ v for v in sorted(di.values())]

python sort dict 总结的更多相关文章

  1. Python中dict详解

    from:http://www.cnblogs.com/yangyongzhi/archive/2012/09/17/2688326.html Python中dict详解 python3.0以上,pr ...

  2. python的dict,set,list,tuple应用详解

    python的dict,set,list,tuple应用详解 本文深入剖析了python中dict,set,list,tuple应用及对应示例,有助于读者对其概念及原理的掌握.具体如下: 1.字典(d ...

  3. Python: sort,sorted,OrderedDict的用法

    Python: sort,sorted,OrderedDict的用法 from http://stqdd.com/archives/427 by 莫亚菜 python对容器内数据的排序有两种,一种是容 ...

  4. Python中dict的特点、更新dict、遍历dict

    dict的第一个特点是查找速度快,无论dict有10个元素还是10万个元素,查找速度都一样.而list的查找速度随着元素增加而逐渐下降. 不过dict的查找速度快不是没有代价的,dict的缺点是占用内 ...

  5. python 字典 dict 该注意的一些操作

    在用python处理dict 的时候,有几个该注意的地方,这里跟大家提一下: 1)操作dict 时,尽量少产生新的列表对象.比如: 遍历dict的时候,如果用 dic = {"a" ...

  6. python字典dict的增、删、改、查操作

    ## python字典dict的增.删.改.查操作dict = {'age': 18, 'name': 'jin', 'sex': 'male', }#增# dict['heigh'] = 185 # ...

  7. Python 字典 dict() 函数

    描述 Python 字典 dict() 函数用于创建一个新的字典,用法与 Pyhon 字典 update() 方法相似. 语法 dict() 函数函数语法: dict(key/value) 参数说明: ...

  8. Python 基础 Dict 和 Set 类型

    python 什么是dict 例如: d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 } 我们把名称称为key,对应的成绩称为value,dic就是通过key 来查找 ...

  9. python实现dict版图遍历

    python实现dict版图遍历的示例. 代码: #_*_coding:utf_8_import sysimport osclass Graph(): def __init__(self, V, E) ...

随机推荐

  1. ubuntu android 设备识别 Setting up a Device for Development

    参考: http://developer.android.com/tools/device.html   lsusb Bus 001 Device 004: ID 18d1:9025 Google I ...

  2. If the parts of an organization (e.g., teams, departments, or subdivisions) do not closely reflect the essential parts of the product, or if the relationship between organizations do not reflect the r

    https://en.wikipedia.org/wiki/Conway%27s_law

  3. combined with the Referer header, to potentially build an exhaustive data set of user profiles and browsing habits Client Identification

    w https://www.zhihu.com/question/35307626 w 0-客户端(附加用户信息)首次请求服务端--->服务端生成session(有唯一性).session_id ...

  4. Spark源码分析 – Checkpoint

    CP的步骤 1. 首先如果RDD需要CP, 调用RDD.checkpoint()来mark 注释说了, 这个需要在Job被执行前被mark, 原因后面看, 并且最好选择persist这个RDD, 否则 ...

  5. 使用MFC做D3D的框架

    转载请注明出处http://www.cnblogs.com/CAION/p/3192111.html (程序运行时是和其他程序挺像 = =,但我保证这是原创的) 1.将D3D的初始化,渲染等等一些行为 ...

  6. python学习笔记(四)— 函数

    一.函数是什么? 函数一词来源于数学,但编程中的「函数」概念,与数学中的函数是有很大不同的,编程中的函数在英文中也有很多不同的叫法.在BASIC中叫做subroutine(子过程或子程序),在Pasc ...

  7. MapReduce学习笔记

    一.MapReduce概述 MapReduce 是 Hadoop 的核心组成, 是专用于进行数据计算的,是一种分布式计算模型.由Google提出,主要用于搜索领域,解决海量数据的计算问题. MapRe ...

  8. wordpress 主题开发

    https://yusi123.com/3205.html https://themeshaper.com/2012/10/22/the-themeshaper-wordpress-theme-tut ...

  9. sql 区分大小写

    sql server默认是不区分大小写的. 要查看sqlserver数据库是否区分大小写,我么可以查看系统存储过程sys.sp_server_info exec sys.sp_server_info ...

  10. Spring第六弹—-依赖注入之使用构造器注入与使用属性setter方法注入

    所谓依赖注入就是指:在运行期,由外部容器动态地将依赖对象注入到组件中. 使用构造器注入   1 2 3 4 <constructor-arg index=“0” type=“java.lang. ...