【发布时间】:2018-12-31 11:49:27
【问题描述】:
我做了一个小程序并测试它确实保持顺序。但是,我仍然想确保 deepcopy 能够做到这一点。
import copy
import collections
a_dict = collections.OrderedDict()
a_dict['m'] = 10
a_dict['u'] = 15
a_dict['c'] = 5
a_dict['h'] = 25
a_dict['a'] = 55
a_dict['s'] = 30
print(a_dict)
other_dict = copy.deepcopy(a_dict)
other_dict['g'] = 75
other_dict['r'] = 35
print(other_dict)
这个程序的输出是
OrderedDict([('m', 10), ('u', 15), ('c', 5), ('h', 25), ('a', 55), ('s', 30)])
OrderedDict([('m', 10), ('u', 15), ('c', 5), ('h', 25), ('a', 55), ('s', 30), ('g', 75), ('r', 35)])
【问题讨论】:
-
您必须查看 c 代码以查看 deepcopy 是否能够复制定义ordereddicts 顺序的链表。我猜这是可行的。
标签: python deep-copy ordereddictionary