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

  1. <small id='F1lX3'></small><noframes id='F1lX3'>

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

      如何深拷贝一个列表?

      时间:2023-08-30
      <legend id='5Nsja'><style id='5Nsja'><dir id='5Nsja'><q id='5Nsja'></q></dir></style></legend>
      • <bdo id='5Nsja'></bdo><ul id='5Nsja'></ul>

            <tbody id='5Nsja'></tbody>

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

                <small id='5Nsja'></small><noframes id='5Nsja'>

              • 本文介绍了如何深拷贝一个列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                E0_copy = list(E0)之后,我猜E0_copyE0的深拷贝,因为id(E0) 不等于 id(E0_copy).然后我在循环中修改了E0_copy,为什么后面的E0就不一样了?

                After E0_copy = list(E0), I guess E0_copy is a deep copy of E0 since id(E0) is not equal to id(E0_copy). Then I modify E0_copy in the loop, but why is E0 not the same after?

                E0 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
                for k in range(3):
                    E0_copy = list(E0)
                    E0_copy[k][k] = 0
                    #print(E0_copy)
                print E0  # -> [[0, 2, 3], [4, 0, 6], [7, 8, 0]]
                

                推荐答案

                E0_copy 不是深拷贝.您不会使用 list() 进行深层复制.(list(...)testList[:] 都是浅拷贝.)

                E0_copy is not a deep copy. You don't make a deep copy using list(). (Both list(...) and testList[:] are shallow copies.)

                您使用 copy.deepcopy(...) 用于深度复制列表.

                You use copy.deepcopy(...) for deep copying a list.

                deepcopy(x, memo=None, _nil=[])
                    Deep copy operation on arbitrary Python objects.
                

                请参阅以下片段 -

                >>> a = [[1, 2, 3], [4, 5, 6]]
                >>> b = list(a)
                >>> a
                [[1, 2, 3], [4, 5, 6]]
                >>> b
                [[1, 2, 3], [4, 5, 6]]
                >>> a[0][1] = 10
                >>> a
                [[1, 10, 3], [4, 5, 6]]
                >>> b   # b changes too -> Not a deepcopy.
                [[1, 10, 3], [4, 5, 6]]
                

                现在看deepcopy操作

                >>> import copy
                >>> b = copy.deepcopy(a)
                >>> a
                [[1, 10, 3], [4, 5, 6]]
                >>> b
                [[1, 10, 3], [4, 5, 6]]
                >>> a[0][1] = 9
                >>> a
                [[1, 9, 3], [4, 5, 6]]
                >>> b    # b doesn't change -> Deep Copy
                [[1, 10, 3], [4, 5, 6]]
                

                为了解释,list(...) 不会递归地复制内部对象.它只复制最外层列表,同时仍然引用相同的内部列表,因此,当您改变内部列表时,更改会反映在原始列表和浅表副本中.通过检查 id(a[0]) == id(b[0]) where b = list(a) 可以看到浅拷贝引用了内部列表.

                To explain, list(...) does not recursively make copies of the inner objects. It only makes a copy of the outermost list, while still referencing the same inner lists, hence, when you mutate the inner lists, the change is reflected in both the original list and the shallow copy. You can see that shallow copying references the inner lists by checking that id(a[0]) == id(b[0]) where b = list(a).

                这篇关于如何深拷贝一个列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何在文本中查找搭配,python 下一篇:`sorted(list)` 与 `list.sort()` 有什么区别?

                相关文章

                    <bdo id='xYy4m'></bdo><ul id='xYy4m'></ul>
                  <tfoot id='xYy4m'></tfoot>

                    <legend id='xYy4m'><style id='xYy4m'><dir id='xYy4m'><q id='xYy4m'></q></dir></style></legend>

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

                  1. <small id='xYy4m'></small><noframes id='xYy4m'>