python OrderDict

Posted xiaodebing

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python OrderDict相关的知识,希望对你有一定的参考价值。

# encoding: utf-8
import csv
import collections

d = {banana:3,apple:4,pear:1,orange:2}
print(d)
kd = collections.OrderedDict(sorted(d.items(),key = lambda t:t[0]))
print(key order)
print(kd)
vd = collections.OrderedDict(sorted(d.items(),key = lambda t:t[1]))
print(value order)
print(vd)
print(type(vd))
{‘banana‘: 3, ‘apple‘: 4, ‘pear‘: 1, ‘orange‘: 2}
key order
OrderedDict([(‘apple‘, 4), (‘banana‘, 3), (‘orange‘, 2), (‘pear‘, 1)])
value order
OrderedDict([(‘pear‘, 1), (‘orange‘, 2), (‘banana‘, 3), (‘apple‘, 4)])
<class ‘collections.OrderedDict‘>

Process finished with exit code 0

 

以上是关于python OrderDict的主要内容,如果未能解决你的问题,请参考以下文章

python中的defaultdict和orderdict

python之collections模块(OrderDict,defaultdict)

Python其他数据结构collection模块-namtuple defaultdict deque Queue Counter OrderDict

Python_Day_05 计数器(counter),有序字典(OrderDict),默认字典(defaultdict),可命名元祖(namedtuple),双向队列(deque),单项队列(deuq

python模块之collections random

pyyaml “有序”解析/生成yaml