Skip to content

Commit c9ac5fc

Browse files
authored
Merge pull request yidao620c#131 from xiuyanduan/master
Update p07_keep_dict_in_order.rst
2 parents 27b4c0a + d9b1d82 commit c9ac5fc

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

source/c01/p07_keep_dict_in_order.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616
.. code-block:: python
1717
1818
from collections import OrderedDict
19-
def ordered_dict():
20-
d = OrderedDict()
21-
d['foo'] = 1
22-
d['bar'] = 2
23-
d['spam'] = 3
24-
d['grok'] = 4
25-
# Outputs "foo 1", "bar 2", "spam 3", "grok 4"
26-
for key in d:
27-
print(key, d[key])
19+
20+
d = OrderedDict()
21+
d['foo'] = 1
22+
d['bar'] = 2
23+
d['spam'] = 3
24+
d['grok'] = 4
25+
# Outputs "foo 1", "bar 2", "spam 3", "grok 4"
26+
for key in d:
27+
print(key, d[key])
2828
2929
当你想要构建一个将来需要序列化或编码成其他格式的映射的时候, ``OrderedDict`` 是非常有用的。
3030
比如,你想精确控制以JSON编码后字段的顺序,你可以先使用 ``OrderedDict`` 来构建这样的数据:

0 commit comments

Comments
 (0)