Python字典排序OrderedDict
来源:互联网 发布:中国社会经济数据库 编辑:程序博客网 时间:2024/05/21 12:46
在Python2.7.x版本中, collections类增加了OrderedDict, 用法如下:
pywugw@pywugw-laptop:~$/usr/local/bin/python2.7
Python 2.7b1(r27b1:79927, Apr 26 2010, 11:44:19)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits"or "license" for more information.
>>>from collections import OrderedDict
>>> d= {'banana': 3,'apple':4,'pear': 1,'orange': 2}
#按key排序
>>>OrderedDict(sorted(d.items(), key=lambda t: t[0]))
OrderedDict([('apple',4),('banana', 3),('orange', 2),('pear', 1)])
#按value排序
>>>OrderedDict(sorted(d.items(), key=lambda t: t[1]))
OrderedDict([('pear',1),('orange', 2),('banana', 3),('apple', 4)])
#按key的长度排序
>>>OrderedDict(sorted(d.items(), key=lambda t: len(t[0])))
OrderedDict([('pear',1),('apple', 4),('orange', 2),('banana', 3)])
- Python字典排序OrderedDict
- python 有序字典OrderedDict
- python中的有序字典OrderedDict
- python OrderedDict
- Python OrderedDict
- python 2-6 如何让字典保持有序collections.OrderedDict
- Python的collections模块中的OrderedDict有序字典
- python中排序,函数sort、sorted、argsort,collections.OrderedDict类
- Python collections OrderedDict
- python OrderedDict用法
- python——OrderedDict 、importlib
- python中OrderedDict的使用
- python字典排序
- python字典排序
- python字典排序
- python 字典排序
- python 字典排序
- python字典排序
- boa 源码分析(2)--关于Boa和CGI
- android面试题总结加强再加强版(三)
- 【phpcms-v9】缓存目录caches下的各个文件的作用
- Google Map开发系列(二)——使用谷歌地图API应该有所了解的一些技术外背景
- Jquery中的parent()与parents()取父元素的区别
- Python字典排序OrderedDict
- Oracle 时间相减
- 《一个操作系统的实现》阅读笔记 之 保护模式
- 主元分析(PCA)理论分析及应用
- android面试题总结加强再加强版(四)
- C# 中问号的使用
- Android APK反编译详解(附图)
- 西班牙著名品牌“ZARA”演绎供应链极速传说
- webservice 常用接口