<small id='07OTU'></small><noframes id='07OTU'>

    • <bdo id='07OTU'></bdo><ul id='07OTU'></ul>
    <tfoot id='07OTU'></tfoot>

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

      切片操作会给我一个深拷贝还是浅拷贝?

      时间:2023-08-31

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

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

              <tbody id='la3I9'></tbody>
          1. <i id='la3I9'><tr id='la3I9'><dt id='la3I9'><q id='la3I9'><span id='la3I9'><b id='la3I9'><form id='la3I9'><ins id='la3I9'></ins><ul id='la3I9'></ul><sub id='la3I9'></sub></form><legend id='la3I9'></legend><bdo id='la3I9'><pre id='la3I9'><center id='la3I9'></center></pre></bdo></b><th id='la3I9'></th></span></q></dt></tr></i><div id='la3I9'><tfoot id='la3I9'></tfoot><dl id='la3I9'><fieldset id='la3I9'></fieldset></dl></div>
              <bdo id='la3I9'></bdo><ul id='la3I9'></ul>
              • 本文介绍了切片操作会给我一个深拷贝还是浅拷贝?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                官方 Python 文档说在 Python 中使用切片运算符和赋值制作切片列表的浅表副本.

                The official Python docs say that using the slicing operator and assigning in Python makes a shallow copy of the sliced list.

                但是当我写代码例如:

                o = [1, 2, 4, 5]
                p = o[:]
                

                当我写的时候:

                id(o)
                id(p)
                

                我得到不同的 id,并且附加一个列表不会反映在另一个列表中.是不是在创建深拷贝,还是我哪里出错了?

                I get different id's and also appending one one list does not reflect in the other list. Isn't it creating a deep copy or is there somewhere I am going wrong?

                推荐答案

                您正在创建一个 副本,因为嵌套值不会被复制,而只是被引用.deep 副本也会创建列表引用的值的副本.

                You are creating a shallow copy, because nested values are not copied, merely referenced. A deep copy would create copies of the values referenced by the list too.

                演示:

                >>> lst = [{}]
                >>> lst_copy = lst[:]
                >>> lst_copy[0]['foo'] = 'bar'
                >>> lst_copy.append(42)
                >>> lst
                [{'foo': 'bar'}]
                >>> id(lst) == id(lst_copy)
                False
                >>> id(lst[0]) == id(lst_copy[0])
                True
                

                这里没有复制嵌套字典;它仅被两个列表引用.新元素 42 未共享.

                Here the nested dictionary is not copied; it is merely referenced by both lists. The new element 42 is not shared.

                请记住,Python 中的一切都是对象,名称和列表元素只是对这些对象的引用.列表的副本会创建一个新的外部列表,但新列表仅接收对完全相同的对象的引用.

                Remember that everything in Python is an object, and names and list elements are merely references to those objects. A copy of a list creates a new outer list, but the new list merely receives references to the exact same objects.

                适当的深拷贝会递归地创建列表中包含的每个对象的新副本:

                A proper deep copy creates new copies of each and every object contained in the list, recursively:

                >>> from copy import deepcopy
                >>> lst_deepcopy = deepcopy(lst)
                >>> id(lst_deepcopy[0]) == id(lst[0])
                False
                

                这篇关于切片操作会给我一个深拷贝还是浅拷贝?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Pandas:链式作业 下一篇:如何在 Python 中对函数进行深度复制?

                相关文章

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

                  • <bdo id='nV9Ms'></bdo><ul id='nV9Ms'></ul>
                2. <legend id='nV9Ms'><style id='nV9Ms'><dir id='nV9Ms'><q id='nV9Ms'></q></dir></style></legend>

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