<tfoot id='lAoY6'></tfoot>

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

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

        如何制作复杂列表的完全非共享副本?(深拷贝是不够的)

        How to make a completely unshared copy of a complicated list? (Deep copy is not enough)(如何制作复杂列表的完全非共享副本?(深拷贝是不够的))
        1. <tfoot id='hybWV'></tfoot>
        2. <small id='hybWV'></small><noframes id='hybWV'>

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

                    <tbody id='hybWV'></tbody>
                  本文介绍了如何制作复杂列表的完全非共享副本?(深拷贝是不够的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  看看这个 Python 代码:

                  Have a look at this Python code:

                  a = [1, 2, 3]
                  b = [4, 5, 6]
                  c = [[a, b], [b, a]] # [[[1, 2, 3], [4, 5, 6]], [[4, 5, 6], [1, 2, 3]]]
                  c[0][0].append(99)   # [[[1, 2, 3, 99], [4, 5, 6]], [[4, 5, 6], [1, 2, 3, 99]]]
                  

                  注意修改 c 的一个元素是如何在任何地方修改它的.也就是说,如果 99 附加到 c[0][0],它也附加到 c[1][1].我猜这是因为 Python 巧妙地引用 相同的对象 用于 c[0][0]c[1][1].(就是他们的id()是一样的.)

                  Notice how modifying one element of c modifies that everywhere. That is, if 99 is appended to c[0][0], it is also appended to c[1][1]. I am guessing this is because Python is cleverly referring to the same object for c[0][0] and c[1][1]. (That is their id() is the same.)

                  问题:是否可以对 c 进行一些操作,以便可以在本地安全地修改其列表元素?上面只是一个例子,我真正的问题有一个更复杂的列表,但有一个类似的问题.

                  Question: Is there something that can be done to c so that its list elements can be safely locally modified? Above is just an example, my real problem has a list much more complicated, but having a similar problem.

                  (抱歉,上面的问题格式不正确.Python 大师请随时修改问题或标签以更好地表达此查询.)

                  (Sorry for the poorly formed question above. Python gurus please feel free to modify the question or tags to better express this query.)

                  推荐答案

                  要将现有的列表列表转换为不共享任何内容的列表,您可以递归地复制该列表.

                  To convert an existing list of lists to one where nothing is shared, you could recursively copy the list.

                  deepcopy 是不够的,因为它将按原样复制结构,将 internal 引用保留为引用,而不是副本.

                  deepcopy will not be sufficient, as it will copy the structure as-is, keeping internal references as references, not copies.

                  def unshared_copy(inList):
                      if isinstance(inList, list):
                          return list( map(unshared_copy, inList) )
                      return inList
                  
                  alist = unshared_copy(your_function_returning_lists())
                  

                  请注意,这假定数据以列表列表的形式返回(任意嵌套).如果容器是不同类型的(例如 numpy 数组、字典或用户类),您可能需要更改它.

                  Note that this assumes the data is returned as a list of lists (arbitrarily nested). If the containers are of different types (eg. numpy arrays, dicts, or user classes), you may need to alter this.

                  这篇关于如何制作复杂列表的完全非共享副本?(深拷贝是不够的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 与否?)

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

                            <tfoot id='PZLIj'></tfoot>

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