1. <legend id='dI76B'><style id='dI76B'><dir id='dI76B'><q id='dI76B'></q></dir></style></legend>

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

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

        Python:deepcopy(list) vs new_list = old_list[:]

        Python: deepcopy(list) vs new_list = old_list[:](Python:deepcopy(list) vs new_list = old_list[:])
      3. <i id='LgmFW'><tr id='LgmFW'><dt id='LgmFW'><q id='LgmFW'><span id='LgmFW'><b id='LgmFW'><form id='LgmFW'><ins id='LgmFW'></ins><ul id='LgmFW'></ul><sub id='LgmFW'></sub></form><legend id='LgmFW'></legend><bdo id='LgmFW'><pre id='LgmFW'><center id='LgmFW'></center></pre></bdo></b><th id='LgmFW'></th></span></q></dt></tr></i><div id='LgmFW'><tfoot id='LgmFW'></tfoot><dl id='LgmFW'><fieldset id='LgmFW'></fieldset></dl></div>
        1. <small id='LgmFW'></small><noframes id='LgmFW'>

            <legend id='LgmFW'><style id='LgmFW'><dir id='LgmFW'><q id='LgmFW'></q></dir></style></legend>
            <tfoot id='LgmFW'></tfoot>
                <tbody id='LgmFW'></tbody>
                <bdo id='LgmFW'></bdo><ul id='LgmFW'></ul>

                • 本文介绍了Python:deepcopy(list) vs new_list = old_list[:]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在做练习 #9,来自 http://openbookproject.net/thinkcs/python/english2e/ch09.html 并且遇到了一些没有意义的东西.

                  I'm doing exercise #9 from http://openbookproject.net/thinkcs/python/english2e/ch09.html and have ran into something that doesn't make sense.

                  该练习建议使用 copy.deepcopy() 使我的任务更轻松,但我不明白它是如何做到的.

                  The exercise suggests using copy.deepcopy() to make my task easier but I don't see how it could.

                  def add_row(matrix):
                      """
                          >>> m = [[0, 0], [0, 0]]
                          >>> add_row(m)
                          [[0, 0], [0, 0], [0, 0]]
                          >>> n = [[3, 2, 5], [1, 4, 7]]
                          >>> add_row(n)
                          [[3, 2, 5], [1, 4, 7], [0, 0, 0]]
                          >>> n
                          [[3, 2, 5], [1, 4, 7]]
                      """
                  
                      import copy
                      # final = copy.deepcopy(matrix)  # first way
                      final = matrix[:]                # second way
                      li = []
                      for i in range(len(matrix[0])):
                          li.append(0)
                      # return final.append(li)  # why doesn't this work?
                      final.append(li)            # but this does
                      return final
                  

                  我很困惑为什么本书建议在简单的 list[:] 复制它时使用 deepcopy().我用错了吗?我的功能完全不正常吗?

                  I'm confused why the book suggests using deepcopy() when a simple list[:] copies it. Am I using it wrong? Is my function completely out of wack?

                  我也有一些混乱的返回值.问题是上面代码中的文档.

                  I also have some confusion returning values. the question is documents in the code above.

                  TIA

                  推荐答案

                  你问了两个问题:

                  matrix[:] 是一个浅拷贝——它只拷贝直接存储在其中的元素,不会递归地拷贝数组或其他引用的元素自身之内.这意味着:

                  matrix[:] is a shallow copy -- it only copies the elements directly stored in it, and doesn't recursively duplicate the elements of arrays or other references within itself. That means:

                  a = [[4]]
                  b = a[:]
                  a[0].append(5)
                  print b[0] # Outputs [4, 5], as a[0] and b[0] point to the same array
                  

                  如果您将对象存储在 a 中也会发生同样的情况.

                  The same would happen if you stored an object in a.

                  deepcopy() 自然是一个deep copy——它递归地复制每个元素,一直沿树向下:

                  deepcopy() is, naturally, a deep copy -- it makes copies of each of its elements recursively, all the way down the tree:

                  a = [[4]]
                  c = copy.deepcopy(a)
                  a[0].append(5)
                  print c[0] # Outputs [4], as c[0] is a copy of the elements of a[0] into a new array
                  

                  返回

                  return final.append(li) 与调用 append 并返回 final 不同,因为 list.append 不返回列表对象本身, 它返回 None

                  Returning

                  return final.append(li) is different from calling append and returning final because list.append does not return the list object itself, it returns None

                  这篇关于Python:deepcopy(list) vs new_list = old_list[:]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 与否?)
                      <tbody id='AhU07'></tbody>
                    1. <small id='AhU07'></small><noframes id='AhU07'>

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

                          1. <tfoot id='AhU07'></tfoot>

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