Home Python 有序字典 OrderedDict
Post
Cancel

Python 有序字典 OrderedDict

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from collections import OrderedDict

d = OrderedDict()
d['foo'] = 1
d['bar'] = 2
d['spam'] = 3
d['grok'] = 4
# Outputs "foo 1", "bar 2", "spam 3", "grok 4"
for key in d:
    print(key, d[key])
 
# 要构建一个需要序列化或编码成其他格式的映射的时候,OrderedDict 是非常有用
import json
json.dumps(d) # '{"foo": 1, "bar": 2, "spam": 3, "grok": 4}'

注意: OrderedDict 内部维护着一个根据键插入顺序排序的双向链表。每次当一个新的元素插入进来的时候,它会被放到链表的尾部。对于一个已经存在的键的重复赋值不会改变键的顺序。需要注意的是,一个 OrderedDict 的大小是一个普通字典的两倍,因为它内部维护着另外一个链表,所以使用(尤其是数据太大)时有必要考虑内存消耗的问题。

This post is licensed under CC BY 4.0 by the author.