Deep copy and shallow copy of Python list-and method of initializing blank list (1)

Source: Internet
Author: User
Tags shallow copy python list

One of the most bizarre "bugs" encountered at a time when you were doing a question: what I want to do is initialize shape, but the value is "empty" list, and it's a two-dimensional list, and that's what I did:

l1=[[""]*3]*3print  l1[["", ["] ['], [']]

You can see the shape of this matrix, which can be understood as a n*n matrix.

Put the title out--the topic is very simple, input a n*n matrix, output The matrix clockwise rotation of the matrix after 90°. At the beginning my code is as follows:

class Rotate:     def Rotatematrix (self, Mat, n):         # Write code        here v=[[""]*3]*3 for in          Range (n):             for in range (N):                v[i][j]=mat[n-j-1][i]        return V

The test data entered are:

[[1,2,3],[4,5,6],[7,8,9]]

The correct output should be:

[[7,4,1],[8,5,2],[9,6,3]]

And I ran the result:

[[9,6,3],[9,6,3],[9,6,3]]

Very puzzled, the output of the three sub-array is actually the same result

Later on the original code slightly modified, the "empty" list of the initialization is defined as: v=[[ 0 for i in range(n)]  forj in range(n)],即:

class Rotate:     def Rotatematrix (self, Mat, n):         # Write code        here  for inch  for inch range (n)]          for inch range (N):              for inch range (N):                v[i][j]=mat[n-j-1][i]        return V

The correct result is finally output [[9,6,3],[9,6,3],[9,6,3]], and the code test passes.

Here's a comparison of the two initialization methods:

At least in the output look, long is the same, is there really no difference between the L1 and L2? Here we introduce a common function------ID (x), which represents the memory address of the variable inside the parentheses

Let's take a look at the memory address of the L1,L2 list:

Shocked! Here you can see that the memory address of the L1 three sub list is exactly the same! and L2 's sub list is located in the memory address is different!??

Our team L1,l2 's sub list is extend, respectively, as follows:

We were only l1/l2 the second sub list of the extend operation, but the result is L1 three sub-arrays are affected, and L2 is only the second sub-array to implement the extend operation.

Then look at the memory address of each sub-list:

In line with the results just now, L1 's three sub-arrays are in the same memory address, and L2 are different.

Use a direct "=" To assign a L1 to a sub-list?

At this point, only the assigned l1[1] is successfully assigned, the other two are unchanged, and the memory address of L1[1] has changed, and L1[0],l1[2] remains the same

Here's why you can summarize:

1. Use * for list "copy", the resulting new array and the copied array is actually in the same memory address, such a copy of the way to become a latent copy

2. The result of a shallow copy initialization is that the copied objects are affected by the same effect when they are related to the copied object, because they are essentially the same list, both at the same address

3. Use "=" To assign a value, the "=" after the content is created into a new memory address, and the "=" left the variable's address to the "=" to the right of the resulting new address

specifically to solve the actual problem, when initializing a "known shape, but internal values to fill" an "empty" list, it is recommended to use a for loop initialization, such as the above mentioned format, that is:

v=[[' for with in range (n)]

Deep copy and shallow copy of Python list-and method of initializing blank list (1)

Related Article

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.