• <legend id='vZe1j'><style id='vZe1j'><dir id='vZe1j'><q id='vZe1j'></q></dir></style></legend>
      <bdo id='vZe1j'></bdo><ul id='vZe1j'></ul>

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

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

        <tfoot id='vZe1j'></tfoot>

        理解 dict.copy() - 浅的还是深的?

        时间:2023-08-31

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

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

              • <tfoot id='Oir7B'></tfoot>

                  <tbody id='Oir7B'></tbody>

                <legend id='Oir7B'><style id='Oir7B'><dir id='Oir7B'><q id='Oir7B'></q></dir></style></legend>
                1. 本文介绍了理解 dict.copy() - 浅的还是深的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在阅读 dict.copy() 的文档时,它说它制作了字典的浅表副本.我正在关注的书(Beazley's Python Reference)也是如此,它说:

                  While reading up the documentation for dict.copy(), it says that it makes a shallow copy of the dictionary. Same goes for the book I am following (Beazley's Python Reference), which says:

                  m.copy() 方法使浅包含在 a 中的项目的副本映射对象并将它们放置在新的映射对象.

                  The m.copy() method makes a shallow copy of the items contained in a mapping object and places them in a new mapping object.

                  考虑一下:

                  >>> original = dict(a=1, b=2)
                  >>> new = original.copy()
                  >>> new.update({'c': 3})
                  >>> original
                  {'a': 1, 'b': 2}
                  >>> new
                  {'a': 1, 'c': 3, 'b': 2}
                  

                  所以我假设这会更新 original 的值(并添加'c':3),因为我正在做一个浅拷贝.就像您为列表做的一样:

                  So I assumed this would update the value of original (and add 'c': 3) also since I was doing a shallow copy. Like if you do it for a list:

                  >>> original = [1, 2, 3]
                  >>> new = original
                  >>> new.append(4)
                  >>> new, original
                  ([1, 2, 3, 4], [1, 2, 3, 4])
                  

                  这按预期工作.

                  既然都是浅拷贝,为什么 dict.copy() 不能像我预期的那样工作?还是我对浅拷贝和深拷贝的理解有缺陷?

                  Since both are shallow copies, why is that the dict.copy() doesn't work as I expect it to? Or my understanding of shallow vs deep copying is flawed?

                  推荐答案

                  所谓的浅拷贝"是指字典的内容不是按值拷贝,而是创建一个新的引用.

                  By "shallow copying" it means the content of the dictionary is not copied by value, but just creating a new reference.

                  >>> a = {1: [1,2,3]}
                  >>> b = a.copy()
                  >>> a, b
                  ({1: [1, 2, 3]}, {1: [1, 2, 3]})
                  >>> a[1].append(4)
                  >>> a, b
                  ({1: [1, 2, 3, 4]}, {1: [1, 2, 3, 4]})
                  

                  相比之下,深拷贝将按值复制所有内容.

                  In contrast, a deep copy will copy all contents by value.

                  >>> import copy
                  >>> c = copy.deepcopy(a)
                  >>> a, c
                  ({1: [1, 2, 3, 4]}, {1: [1, 2, 3, 4]})
                  >>> a[1].append(5)
                  >>> a, c
                  ({1: [1, 2, 3, 4, 5]}, {1: [1, 2, 3, 4]})
                  

                  所以:

                  1. b = a:引用赋值,使ab指向同一个对象.

                  1. b = a: Reference assignment, Make a and b points to the same object.

                  b = a.copy():浅拷贝,ab会变成两个孤立的对象,但是它们的内容仍然共享相同的参考

                  b = a.copy(): Shallow copying, a and b will become two isolated objects, but their contents still share the same reference

                  b = copy.deepcopy(a):深拷贝,ab的结构和内容完全隔离.

                  b = copy.deepcopy(a): Deep copying, a and b's structure and content become completely isolated.

                  这篇关于理解 dict.copy() - 浅的还是深的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:`sorted(list)` 与 `list.sort()` 有什么区别? 下一篇:Pandas:链式作业

                  相关文章

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

                    <bdo id='ZnrI7'></bdo><ul id='ZnrI7'></ul>
                  <legend id='ZnrI7'><style id='ZnrI7'><dir id='ZnrI7'><q id='ZnrI7'></q></dir></style></legend>

                    <tfoot id='ZnrI7'></tfoot>

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