嵌套字典copy()或deepcopy()? [英] Nested dictionaries copy() or deepcopy()?

查看:52
本文介绍了嵌套字典copy()或deepcopy()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将字典模板存储在大多数功能将使用的代码的开头:

I tried to store a dictionary template at the beginning of my code that most of the functions will use:

  • 字典:键=客户名称,值= Dictionary2
  • 字典2:键=用户名,值=无

我把所有的客户和他们的用户都塞满了.然后,代码的每个部分都可以复制此字典并产生其自己的输出.目标是每个输出将具有相同的基本"字典结构,例如可以修改None的模板.

I filled it with all our clients and their users. Then each part of the code can copy this dictionary and produces it's owns outputs. The goal is that each output will have the same "base" dictionary structure like a template where None can be modified.

对于使用该词典的每个过程,我使用以下内容:

For each process using this dictionnary I use the following :

process1dict = clientdict 
# processing 1
output1dict = ... #modified version of original clientdict, the None values have been replaced by dictionaries/lists

process2dict = clientdict
# processing 2
output2dict = ... #same here but could be different

我遇到的问题是,每次复制到进程中时,先后顺序都会改变!我注意到,由于我的初始 dict 中的 None 值,它在每个过程之后都会改变(取决于每个过程的输出).

The problem that I have is that the cliendict changes each time it is copied into a process! I noticed that because of the None value in my initial cliendict it changes after each process (depending on the output of each one of course).

编辑:我找到了副本库,但是 copy()似乎对我的情况没有帮助.我将尝试使用deepcopy(),但是为什么 copy()无效?为什么 deepcopy()将?

I found the copy library but copy() seems to not help my case. I will try out the deepcopy() but why did copy() didn't worked? And why deepcopy() will?

推荐答案

使用字典或列表等可变集合进行分配时,默认情况下不会创建该对象的副本–即,将某些dict b 分配给另一个dict a ,会创建从 b 到原始对象 a ,这样,当您对 b 进行突变时,您也会间接地对 a 进行突变.

When you're working with a mutable collection like a dictionary or a list, and you perform an assignment, you are not creating a copy of that object by default – i.e., the assignment of some dict b to another dict a creates a reference from b to the original object a, such that when you mutate b you indirectly also mutate a.

请参见以下基本示例:

>>> orig = {"a": 1, "b": 2}
>>> new = orig
>>> new["a"] = 9
>>> orig
{'a': 9, 'b': 2}
>>> new
{'a': 9, 'b': 2}
>>> new is orig
True

要解决此问题并使 new orig 字典分开的对象不会相互引用,请创建

To fix this and keep the new and orig dictionaries separate objects that do not reference each other, make a deepcopy of orig when assigning it to new:

>>> import copy
>>> orig = {"a": 1, "b": 2}
>>> new = copy.deepcopy(orig)
>>> new["a"] = 9
>>> orig
{'a': 1, 'b': 2}
>>> new
{'a': 9, 'b': 2}
>>> new is orig
False

此外,这是上面链接到的Python文档的tl; dr:

Also, here's a tl;dr for the Python documentation linked to above:

Python中的赋值语句不复制对象,它们在目标和对象之间创建绑定.对于可变或包含可变项的集合,有时需要一个副本,因此一个副本可以更改一个副本而无需更改另一个副本.

Assignment statements in Python do not copy objects, they create bindings between a target and an object. For collections that are mutable or contain mutable items, a copy is sometimes needed so one can change one copy without changing the other.

这篇关于嵌套字典copy()或deepcopy()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆