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 ...
随机推荐
- seafile修改
---恢复内容开始--- [root@seafile yunpan]# vim /yunpan/installed/seahub/seahub/templates/footer.html ---恢复内 ...
- 无法完成安装:'unsupported configuration: hda-duplex not supported in this QEMU binary'
Linux 有问必答:如何修复"hda-duplex not supported in this QEMU binary" 编译自:http://ask.xmodulo.com/h ...
- solr5.5教程-tomcat布署(2)
tomcat 布署成功后,接下来就是使用了. 首先要创建一个core. 1.选择右侧菜单, Core Admin -> Add Core. 注意:name自己定义,instanceDir要填写上 ...
- Windows Server 2008 R2 配置Exchange 2010邮件服务器
windows server 服务器系统搭建邮件服务器一般两种情况: 1:Winmail server 软件 2:Exchange 参考教程:http://www.cnblogs.com/zhongw ...
- ububtu 14.04 问题集合
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4168168.html 1.Chromium 中的flash插件问题: sudo apt-ge ...
- qt5.5实现 记事本程序
最近由于要做Qt相关的毕业设计课题,以前对Qt完全不了解,对于客户端图形界面程序,也只对Windows下的MFC熟悉, 所以,由于Qt的跨平台特性和相对比较纯的C++的特点,就准备学习一下吧.这两天逛 ...
- jquery中选择ID以什么字符开头的匹配主要用于多个上传控件的时候,id无法使用,而且class不起作用的时候
$("[id^=remark]")选择ID以remark开头的所有数据进行匹配
- HTML表单的问题
1. 表单一定要放在<form>标签里面,就会有错误. 像<textarea>单独放的时候,单击的时候不会出现在开始位置,而是在任意单击的地方开始.
- C#访问配置文件
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.R ...
- [leetcode]_Minimum Depth of Binary Tree
第五道树题,10分钟之内一遍AC.做树题越来越有feel~ 题目:求一棵树从root结点到叶子结点的最短路径. 思路:仍然是递归.如果一个结点的左右子树任意一边为Null,该子树的minDepth应为 ...