Reference issue when Python list is constructed

Source: Internet
Author: User

Used to pay no attention to the Python object reference, usually did not encounter such a problem, yesterday in this small problem tangled half a day. What a damn.

Let me explain my purpose, I have a list of 16 elements, and each element is a small list. I want every four sub-list to be one unit, changing the first element of each sub-list to the value I want.

The code is as follows

>>>a = range (1,5)

>>>b = [[0]*3]*16

>>>for I in range (4):
each = B[i*4: (i+1)
For item in each:
Item[0]=a[i]
Print it

[1, 0, 0]
[1, 0, 0]
[1, 0, 0]
[1, 0, 0]
[2, 0, 0]
[2, 0, 0]
[2, 0, 0]
[2, 0, 0]
[3, 0, 0]
[3, 0, 0]
[3, 0, 0]
[3, 0, 0]
[4, 0, 0]
[4, 0, 0]
[4, 0, 0]
[4, 0, 0]

Well, I want to see the results, but I do not want to print out, but I printed b out, I go, how to become the following bird like

[[4, 0, 0], [4, 0, 0], [4, 0, 0], [4, 0, 0], [4, 0, 0], [4, 0, 0], [4, 0, 0], [4, 0, 0], [4, 0, 0], [4, 0, 0], [4, 0, 0], [4, 0, 0], [4, 0, 0], [4, 0, 0], [4, 0, 0], [4, 0, 0]]

So I analyzed several times that two for loop, all began to wonder if the computer is bad ... Finally, I did not analyze a result, so I went to open source China that help, the result is that I made a list of the wrong.

b = [[0]*3]*16 This method constructs a list, 16 elements are references to the No. 0 element in the list, and does not really open up new space: B[0] is b[1] will return True,is as if the object's ID to determine whether the same object, i.e. b[0] and b[1] The ID is the same.

So this list-building approach really doesn't reach my goal? No,no,no! There are shallow and deep copies in Python replication. This method of construction is that the elements that follow in the list are shallow and duplicate the first element. We can do this by introducing a deep copy in the later for loop.

>>>b1= []
>>> for I in range (4):
each = Copy.deepcopy (b[i*4: (i+1)))
For item in each:
Item[0]=a[i]
B1.append (item)


>>> B1
>>>[[1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0], [2, 0, 0], [2, 0, 0], [2, 0, 0], [2, 0, 0], [3, 0, 0], [3, 0, 0], [3, 0, 0], [3, 0, 0], [4, 0, 0], [4, 0, 0], [4, 0, 0], [4, 0, 0]]

However, this method does not solve the problem of money copying when the list is constructed, because the B-list or all elements are the same as the No. 0 element.

Let's change the method of constructing the list, and recursively construct the list.

>>> B = [[0]*3 for I in range (16)]
>>> b
>>>[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]

Although it looks like the previous method is a bird-like, but this is only a representation.

Use B[0] is b[1] to verify that the return is false. This means that the construction method will open up new space for each element in the derivation, that is, the IDs of B[0] and b[1] are different.

For I in range (4):
each = B[i*4: (i+1)
For item in each:
Item[0]=a[i]


>>> b
>>>[[1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0], [2, 0, 0], [2, 0, 0], [2, 0, 0], [2, 0, 0], [3, 0, 0], [3, 0, 0], [3, 0, 0], [3, 0, 0], [4, 0, 0], [4, 0, 0], [4, 0, 0], [4, 0, 0]]

When constructed in this way, no call to deepcopy this method can achieve the effect ...

To summarize: When constructing a list, note that using the multiplication sign construct is money copying, all objects refer to the No. 0 element, and the use of the list deduction directly opens up new space, no replication is not present.

To learn about Python replication and object references, refer to http://www.cnblogs.com/BeginMan/p/3197649.html

Reference issue when Python list is constructed

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.