• <legend id='FOqDt'><style id='FOqDt'><dir id='FOqDt'><q id='FOqDt'></q></dir></style></legend>
    <i id='FOqDt'><tr id='FOqDt'><dt id='FOqDt'><q id='FOqDt'><span id='FOqDt'><b id='FOqDt'><form id='FOqDt'><ins id='FOqDt'></ins><ul id='FOqDt'></ul><sub id='FOqDt'></sub></form><legend id='FOqDt'></legend><bdo id='FOqDt'><pre id='FOqDt'><center id='FOqDt'></center></pre></bdo></b><th id='FOqDt'></th></span></q></dt></tr></i><div id='FOqDt'><tfoot id='FOqDt'></tfoot><dl id='FOqDt'><fieldset id='FOqDt'></fieldset></dl></div>

      <bdo id='FOqDt'></bdo><ul id='FOqDt'></ul>

    <small id='FOqDt'></small><noframes id='FOqDt'>

    <tfoot id='FOqDt'></tfoot>

        嵌套字典 copy() 还是 deepcopy()?

        Nested dictionaries copy() or deepcopy()?(嵌套字典 copy() 还是 deepcopy()?)

          <small id='wkW8e'></small><noframes id='wkW8e'>

            <bdo id='wkW8e'></bdo><ul id='wkW8e'></ul>

                <tfoot id='wkW8e'></tfoot>

                    <tbody id='wkW8e'></tbody>
                  <i id='wkW8e'><tr id='wkW8e'><dt id='wkW8e'><q id='wkW8e'><span id='wkW8e'><b id='wkW8e'><form id='wkW8e'><ins id='wkW8e'></ins><ul id='wkW8e'></ul><sub id='wkW8e'></sub></form><legend id='wkW8e'></legend><bdo id='wkW8e'><pre id='wkW8e'><center id='wkW8e'></center></pre></bdo></b><th id='wkW8e'></th></span></q></dt></tr></i><div id='wkW8e'><tfoot id='wkW8e'></tfoot><dl id='wkW8e'><fieldset id='wkW8e'></fieldset></dl></div>
                • <legend id='wkW8e'><style id='wkW8e'><dir id='wkW8e'><q id='wkW8e'></q></dir></style></legend>

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

                  问题描述

                  我尝试在代码的开头存储一个字典模板,大多数函数都会使用该模板:

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

                  • 字典:keys = 客户端名称,values = 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
                  

                  我遇到的问题是每次将客户端复制到进程中时都会更改!我注意到,由于我初始 client 中的 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?

                  推荐答案

                  当您使用字典或列表等可变集合并执行赋值时,默认情况下您不会创建该对象的副本– 即,将一些字典 b 分配给另一个字典 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
                  

                  要解决此问题并保持 neworig 字典将不相互引用的对象分开,请创建一个 deepcopy of 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()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)
                  <i id='JFi1E'><tr id='JFi1E'><dt id='JFi1E'><q id='JFi1E'><span id='JFi1E'><b id='JFi1E'><form id='JFi1E'><ins id='JFi1E'></ins><ul id='JFi1E'></ul><sub id='JFi1E'></sub></form><legend id='JFi1E'></legend><bdo id='JFi1E'><pre id='JFi1E'><center id='JFi1E'></center></pre></bdo></b><th id='JFi1E'></th></span></q></dt></tr></i><div id='JFi1E'><tfoot id='JFi1E'></tfoot><dl id='JFi1E'><fieldset id='JFi1E'></fieldset></dl></div>
                • <small id='JFi1E'></small><noframes id='JFi1E'>

                    <tbody id='JFi1E'></tbody>

                  <tfoot id='JFi1E'></tfoot>
                  <legend id='JFi1E'><style id='JFi1E'><dir id='JFi1E'><q id='JFi1E'></q></dir></style></legend>

                            <bdo id='JFi1E'></bdo><ul id='JFi1E'></ul>