Python - 字典按值(value)排序
字典安值排序是一个伪命题.
字典本身是不能被排序的, 已经依照关键字(key)排序, 可是列表(list)和元组(tuple)能够排序, 所以字典须要转换列表后排序.
如
import operator
x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
sorted_x = sorted(x.items(), key=operator.itemgetter(1))
则是转换列表后的排序, dict(sorted_x) == x, 列表转换字典.
代码
# -*- coding: utf-8 -*-
import operator
x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
# 依据值排序
sorted_x = sorted(x.items(), key=operator.itemgetter(1))
print 'x = ' + str(x)
print 'sorted_x = ' + str(sorted_x)
print 'dict(sorted_x) = ' + str(dict(sorted_x))
x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
sorted_x = sorted(x.items(), key=operator.itemgetter(0))
print 'x = ' + str(x)
print 'sorted_x = ' + str(sorted_x)
print 'dict(sorted_x) = ' + str(dict(sorted_x))
"""
输出:
x = {0: 0, 1: 2, 2: 1, 3: 4, 4: 3}
sorted_x = [(0, 0), (2, 1), (1, 2), (4, 3), (3, 4)]
dict(sorted_x) = {0: 0, 1: 2, 2: 1, 3: 4, 4: 3}
x = {0: 0, 1: 2, 2: 1, 3: 4, 4: 3}
sorted_x = [(0, 0), (1, 2), (2, 1), (3, 4), (4, 3)]
dict(sorted_x) = {0: 0, 1: 2, 2: 1, 3: 4, 4: 3}
"""
Python - 字典按值(value)排序的更多相关文章
- Python字典按值排序的方法
Python字典按值排序的方法: 法1: (默认升序排序,加 reverse = True 指定为降序排序) # sorted的结果是一个list dic1SortList = sorted( di ...
- python字典键值对转化为相应的变量名和变量值
将python字典键值对转化为相应的变量名和变量值可以使用以下方法: globals().update({"name":"value"}) locals().u ...
- python根据字典的值进行排序:
有一个列表嵌套字典:[{"a": 5}, {"b": 4}, {"c": 1},{"e": 2}, {"d&q ...
- Python 字典 列表 嵌套 复杂排序大全
https://blog.csdn.net/ray_up/article/details/42084863 一: 字典排序 解析: 使用sorted 方法, 排序后的结果为一个元组. 可以字符串排序( ...
- python字典按照value进行排序
先说几个解决的方法,具体的有时间再细说 d = {'a':1,'b':4,'c':2} 字典是这个,然后要对字典按照value进行排序 方法一: sorted(d.items(),key = lamb ...
- python字典按照value进行排序.bak
先说几个解决的方法,具体的有时间再细说 d = {'a':1,'b':4,'c':2} 字典是这个,然后要对字典按照value进行排序 方法一: sorted(d.items(),key = lamb ...
- Python字典按值排序、包含字典的列表按字典值排序的方法
operator.itemgetter函数 operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号(即需要获取的数据在对象中的序号),要注意,operator.i ...
- python关于字典嵌套字典,列表嵌套字典根据值进行排序
python 对于字典嵌套字典, 列表嵌套字典排序 例:列表嵌套自字典:d = [{"name": '张三', 's': 68}, {'name': '李四', 's': 97}] ...
- python技巧 使用值来排序一个字典
In [8]: a={'x':11,'y':22,'c':4} In [9]: import operator In [10]: sorted(a.items(),key=operator.itemg ...
随机推荐
- hdu 6363 bookshelf
题解讲的很清楚了,直接看代码就懂了 题解:http://bestcoder.hdu.edu.cn/blog/2018-multi-university-training-contest-6-solut ...
- 【Henu ACM Round#20 E】Star
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 找规律. 1,13,37.... 6n(n-1) + 1 [代码] #include <bits/stdc++.h> # ...
- [PostCss] Easily Load Google Fonts with PostCSS Font Magician
Configuring Google Fonts can be quite an annoying process to setup. Using Font Magician with PostCSS ...
- 《AndroidStudio每日一贴》11. 重构之提炼为常量
提炼为常量是从暂时变量高速提炼出静态常量,这也是我们经常使用的重构手段. 很多其它有用技巧请查看<AndroidStudio有用指南> 操作步骤: ➤ 菜单条: Refactor -> ...
- ASP.net Web API允许跨域访问解决办法
来源 http://blog.csdn.net/wxg_kingwolfmsncn/article/details/48545099 遇到此跨域访问问题,解决办法如下: 方法一: 1. 在we ...
- 分享一段官date函数用法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Svn备份与Bandizip压缩批处理程序
目的:为了定时备份多个svn仓库数据,使用批处理程序进行备份并Bandizip进行压缩保存到指定位置,操作完成后弹出成功提示. 为了完成以上目标,需要了解以下几个方面: 批处理命令 Svn命令 Ban ...
- Django_高级扩展
- eclipse 安装javaEE插件 和html\xml\jsp编辑器
1 在Eclipse中菜单help选项中选择install new software选项 2 在work with 栏中输入 http://download.eclipse.org/releases/ ...
- CCF模拟题 最大的矩形
最大的矩形 时间限制: 1.0s 内存限制: 256.0MB 问题描述 在横轴上放了n个相邻的矩形,每个矩形的宽度是1,而第i(1 ≤ i ≤ n)个矩形的高度是hi.这n个矩形构成了一个直方 ...