Python - DICT 字典排序 - OrderedDict
官方地址: https://docs.python.org/2/library/collections.html#collections.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)])
不要在意输出时候,OrderedDict在前面,他和字典一样的用,没区别
Python - DICT 字典排序 - OrderedDict的更多相关文章
- [Python] dict字典排序和多条件排序
利用lambda实现排序:要实现多条件排序,只需要依次指定排序的标准,具体实现如下 counter = {'是': 1, '不是': 1, '你': 3} counter_list = sorted( ...
- 深入Python(1): 字典排序 关于sort()、reversed()、sorted()
http://www.cnblogs.com/BeginMan/p/3193081.html 一.Python的排序 1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠 ...
- python中字典排序,列表中的字典排序
python中字典排序,列表中的字典排序 一.使用python模块:operator import operator #首先要导入模块operator x = {1:2, 3:4, 4:3, 2:1, ...
- python中字典排序
一.Python的排序 1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠倒的:相反的:(判决等)撤销的 print list(reversed(['dream','a ...
- [Python] dict字典的浅复制与深复制
Python中针对dict字典有两种复制: (1)浅复制:利用 copy() 或者 dict() :复制后对原dict的内部子对象(方括号[]内元素)进行操作时,由浅复制得到的dict会受该操作影响 ...
- python进阶--字典排序
zip()函数 sorted() 要求对字典中,按值的大小排序 解决方案: 利用zip函数 zip函数介绍: zip函数可以将可迭代对象打包成一个个元组,在python3中返回一个对象,在python ...
- python dict字典常用操作
字典的特性:key唯一无序 '''特性:key唯一:无序''' info = { 'stu1101': "安徽", 'stu1102': "北京", 'stu1 ...
- Python 有序字典(OrderedDict)与 普通字典(dict)
Python 的基础数据类型中的字典类型分为:无序字典 与 有序字典 两种类型 1.无序字典(普通字典): my_dict = dict()my_dict["name"] = &q ...
- python dict sorted 排序
https://www.cnblogs.com/linyawen/archive/2012/03/15/2398292.html 我们知道Python的内置dictionary数据类型是无序的,通过k ...
随机推荐
- Redis 2:简单使用
导读:上一篇博客对于Redis进行了简单的介绍,本篇博客就浅显的说一下Redis的基本操作使用.本次测试的环境是window8.1,呃,没用Linux等其他系统,就下载的window环境的安装包. 一 ...
- docker容器服务器 - centos atomic host
https://blog.inovex.de/docker-a-comparison-of-minimalistic-operating-systems https://github.com/rvyk ...
- Delphi Form的释放和隐藏:free,hide,close
form.Free - 释放Form占用的所有资源.Free后,Form指针不能再使用,除非对Form重新赋值. form.Hide - 隐藏Form.可以调用form.Show再 ...
- 网络编码 GB2312、GBK与UTF-8的区别
GB2312.GBK与UTF-8的区别 这是一个异常经典的问题,有无数的新手站长每天都在百度这个问题,而我,作为一个“伪老手”站长,在明白这个这个问题的基础上,有必要详细的解答一下. 首先,我们要 ...
- grub2
手工启动 set root(hd0,msdos7) linux /boot/vmlinuz-3.9.8-300.fc19.i686.PAE root=/dev/sda7 initrd /boo ...
- IIS服务器环境配置(一)
在开始-> 控制面板 ->打开或关闭功能 IIS 服务器程序 勾选 HTML勾选 完成添加后 重启 确认是否添加上在控制面板的 管理工具中查看
- CentOS-6.4无线上网命令行配置
参考:http://www.vfeelit.com/504.html 待连接的WIFI的ssid为“thm”,密码为12345678,认证方式为WPA2-PSK 1. 检查无线网卡驱动是否安装 ...
- Linux 常用ps命令
(1)查看系统所有进程: 标准格式: ps -e ps -ef ps -eF ps -ely BSD格式: ...
- Hide a Subpage Using PeopleCode
There was a question asked on the forum on how to hid a subpage. There is no Peoplecode function to ...
- Azkaban 作业调度
转载一篇不错的文章:http://www.cnblogs.com/smartloli/p/5191155.html