Shallow copy & deep copy in Python list

Source: Internet
Author: User
Tags shallow copy python list

Today in the writing of a small program using a 2-dimensional array, conveniently written [[0.0]*length]*length, the result for this small error, debugging for more than half an hour,

In fact, before and shallow copy and deep copy has been done to learn and summarize, but the real programming to use this knowledge or fall into the trap. So here's a further summary:

This article uses several examples to illustrate the deep copy and shallow copy of list in Python:

>>> a = [[]]*10>>> a[[], [], [], [], [], [], [], [], [], []]>>> a[0][0] =        c3/>#NO-Traceback (most recent):  "<stdin> "  in <module>indexerror:list Assignment indexout of range >>> A[0].append (1) /c13>>>> a[[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]]

A[0].append (1), if the output of a is somewhat confusing to you, you can refer to this (because the * operation in Python uses a shallow copy).

In the same vein, the following code should all be understood:

>>> A[1].append (9)>>> a[[1, 9], [1, 9], [1, 9], [1, 9], [1, 9], [1, 9], [1, 9], [1, 9] , [1, 9], [1, 9]]a[2][1] =>>> a[[1, 33], [1, 33], [1, 33], [1, 33], [1, 33], [1, 33 ], [1, [1], [1], [1,]]

  Let's analyze It together:

Each element in a (understood as a 2-D array) is a list (understood as a 1-d array), but we need to be aware of each element of a (a[0],a[1],

A[2] ... The same area of memory is used in memory (shallow copy), so any one element (A[0] or a[1]...a[9]) that changes (modifies the value or adds a value) will directly affect

The other elements.

How do I verify that each element in a a[0], a[1],..., a[9] occupies the same area in memory? The ID method can be used to verify:

>>> ID. __doc__ " ID (object), Integer

Return the identity of an object. This was guaranteed to be unique among
Simultaneously existing objects. (Hint:it ' s The object's memory address.) "

>>> a = [[]]*10>>> a[[], [], [], [], [], [], [], [], [], []]>>> ID (a[0])3 071938316L>>> ID (a[1])3071938316L>>> a[0].append (1)>> > a[[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]]>>> ID (a[0])3071938316l
    >>> ID (a[1])3071938316L

So how do we achieve deep replication? In fact , This method has been introduced in the article mentioned earlier:

 for  in range (ten)]>>> c[[], [], [], [], [], [], [], [], [], []]>>> c[0].append (3
   
     c[[3
    ], [], [], [], [], [], [], [], [], []]
   

So far, I think there is one more thing to note:

Be sure to understand who the object of the operation is, for example: [2]*10 gets [2, 2, 2, 2, 2, 2, 2, 2, 2, 2], the object of the *10 operation is [] 2, which means *10

To make the element 2 in the list replicate 10 times. In the same vein [[]]*10] [[], [], [], [], [], [], [], [], [], [], []],*10], [] [], that is, *10

The action causes the element [] in the list to be shallow copied 10 times, and the 10 empty lists occupy the same area in memory (see the ID verification section above).

The following 2 code as a comparison column, easy to see:

>>> a = [2] * 10>>> a[2, 2, 2, 2, 2, 2, 2, 2, 2, 2]>>> ID (a[0])16406  7492>>> ID (a[1])164067492>>> a[0] = 1 #NOTE>>> ID (a[0]) # NOTE164067504>>> ID (a[1])

>>> B = [[]] * 10>>>b[[], [], [], [], [], [], [], [], [], []]>>>ID (b[0])3072965964l>>> ID (b[1])3072965964l>>> B[0].append (10)>>>ID (b[0])3072965964l>>> ID (b[1])3072965964l>>>b[[10], [10], [10], [10], [10], [10], [10], [10], [10], [10]]>>> B[0][0] = 1>>>b[[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]]>>>ID (b[0])3072965964l>>> ID (b[1])3072965964l>>> b[0] = [10]>>>b[[10], [1], [1], [1], [1], [1], [1], [1], [1], [1]]>>> ID (b[1])3072965964L>>> ID (b[0])3072965996L>>>

Shallow copy & deep copy in Python list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.