sort a Python dictionary by value
首先要明确一点,Python的dict本身是不能被sort的,更明确地表达应该是“将一个dict通过操作转化为value有序的列表”
有以下几种方法:
1.
import operator
x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
sorted_x = sorted(x.items(), key=operator.itemgetter(1))
#sorted by value sorted_x = sorted(x.items(), key=operator.itemgetter(0))
#sorted by key
2.
sorted(dict1, key=dict1.get)
3.
sorted(d.items(), key=lambda x: x[1])
4.
sorted([(value,key) for (key,value) in mydict.items()])
5. Use OrderedDict
>>> # regular unsorted dictionary
>>> d = {'banana': 3, 'apple': 4, 'pear': 1, 'orange': 2} >>> # dictionary sorted by key
>>> OrderedDict(sorted(d.items(), key=lambda t: t[0]))
OrderedDict([('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)]) >>> # dictionary sorted by value
>>> OrderedDict(sorted(d.items(), key=lambda t: t[1]))
OrderedDict([('pear', 1), ('orange', 2), ('banana', 3), ('apple', 4)]) >>> # dictionary sorted by length of the key string
>>> OrderedDict(sorted(d.items(), key=lambda t: len(t[0])))
OrderedDict([('pear', 1), ('apple', 4), ('orange', 2), ('banana', 3)])
sort a Python dictionary by value的更多相关文章
- Python dictionary implementation
Python dictionary implementation http://www.laurentluce.com/posts/python-dictionary-implementation/ ...
- Python dictionary 字典 常用法
Python dictionary 字典 常用法 d = {} d.has_key(key_in) # if has the key of key_in d.keys() ...
- python : dictionary changed size during iteration
1. 错误方式 #这里初始化一个dict >>> d = {'a':1, 'b':0, 'c':1, 'd':0} #本意是遍历dict,发现元素的值是0的话,就删掉 >> ...
- algorithm: heap sort in python 算法导论 堆排序
An Python implementation of heap-sort based on the detailed algorithm description in Introduction to ...
- 快速排序算法回顾 --冒泡排序Bubble Sort和快速排序Quick Sort(Python实现)
冒泡排序的过程是首先将第一个记录的关键字和第二个记录的关键字进行比较,若为逆序,则将两个记录交换,然后比较第二个记录和第三个记录的关键字.以此类推,直至第n-1个记录和第n个记录的关键字进行过比较为止 ...
- PythonStudy——Python字典底层实现原理 The underlying implementation principle of Python dictionary
在Python中,字典是通过散列表或说哈希表实现的.字典也被称为关联数组,还称为哈希数组等.也就是说,字典也是一个数组,但数组的索引是键经过哈希函数处理后得到的散列值.哈希函数的目的是使键均匀地分布在 ...
- [leetcode]Sort Colors @ Python
原题地址:https://oj.leetcode.com/problems/sort-colors/ 题意: Given an array with n objects colored red, wh ...
- python dictionary的遍历
d = {'x':1, 'y':3, 'z':2} for k in d: print d[k] 直接遍历k in d的话,遍历的是dictionary的keys. 2 字典的键可以是任何不可变 ...
- Python dictionary 字典
Python字典是另一种可变容器模型,且可存储任意类型对象,如字符串.数字.元组等其他容器模型. 一.创建字典字典由键和对应值成对组成.字典也被称作关联数组或哈希表.基本语法如下: dict = {' ...
随机推荐
- selenium 总结篇,常见方法和页面元素的操作
今天,总结一下selenium怎么操作web页面常见的元素. 主要有: 上传 alter dialog prompt dialog confirm dialog select list radio b ...
- iOS 获取沙盒路径方法
//获取家目录路径的函数: NSString *homeDir = NSHomeDirectory(); //获取Documents目录路径的方法: NSArray *paths = NSSearch ...
- SQL IN ANY ,(all any) 区别
EXITS 和 IN 的区别: 从效率来看: 1) select * from T1 where exists(select 1 from T2 where T1.a=T2.a) ; T1数据量小而T ...
- python--基础学习(五)参数位置传递、关键字传递、包裹传递及解包裹
python系列均基于python3.4环境 1.位置传递和关键字传递 代码示例 #位置传递 def fun(a,b,c): print("a: {0}, b: {1}, c: {2}&qu ...
- hive创建索引
索引是hive0.7之后才有的功能,创建索引需要评估其合理性,因为创建索引也是要磁盘空间,维护起来也是需要代价的 创建索引 hive> create index [index_studentid ...
- linux内核打印数据到串口控制台,printk数据不打印问题
linux内核打印数据到串口控制台问题 原文来源:http://i.cnblogs.com/EditPosts.aspx?opt=1 1.查看当前控制台的打印级别 cat /proc/sys/kern ...
- Daily Scrum Meeting ——SeventhDay
一.Daily Scrum Meeting照片 二.Burndown Chart 三.项目进展 1.发布者各界面的制作 2.报名表.通知表的制作 3.基本完成登陆.注册.忘记密码.联系管理员界面 四. ...
- InstallShield制作升级安装包
1. 在General Information里,修改Product Version为新的版本号,此版本号应大于旧的版本号.点击Product Code右面的{...},获取一个新的Product C ...
- 探索摸寻之XCode 快捷键
注释/反注释 Command+/ 模拟器没有Home键 在模拟器的应用界面 Command+Shift+h 返回上一级 (1920,24寸显示器,因此不是分辨率问题)
- CS: Marshalling and Unmarshalling, Serialization and Unserialization
Link1: https://en.wikipedia.org/wiki/Marshalling_(computer_science) Quote: " Comparison with se ...