Python copy and python copy

Source: Internet
Author: User

Python copy and python copy

First define a list

>>> S = [1, 'Alex ', 'alvin']

>>> S2 = s. copy () use the copy () method to copy and create a new object s2

>>> Print (s2) copies the same content as the s object.

[1, 'Alex ', 'alvin']

>>> S2 [0] = 3 modify the s2 object Value

>>> Print (s2)

[3, 'Alex ', 'alvin'] print s2 and find that the value of s2 object has changed.

>>> Print (s)

[1, 'Alex ', 'alvin'], but the value of the corresponding s object has not changed

Conclusion: The simple copy operation does not affect the value of any object between the newly created object and the Copied object.

 

>>> S = [[1, 2], 'Alex ', 'alvin']

>>> S3 = s. copy () for copying

>>> Print (s)

[[1, 2], 'Alex ', 'alvin']

>>> Print (s3)

[[1, 2], 'Alex ', 'alvin'] The values output by s and s2 are the same before this step.

>>> S3 [1] = 'linux ': Modify the value of the s2 object. Here, the value of s2 in one step changes, but s does not.

>>> Print (s3)

[[1, 2], 'linux ', 'alvin']

>>> Print (s)

[[1, 2], 'Alex ', 'alvin']

>>> S3 [0] [1] = 3 here, the value of the sublist In the s2 list is modified, print s and s2 and find that the values of the Child list of the two objects have changed.

>>> Print (s3)

[[1, 3], 'linux ', 'alvin']

>>> Print (s)

[[1, 3], 'linux ', 'alvin']

Note: If the Copied object is only an independent list and there is no nested sublist in this object, a completely independent new object will be created during the shortest copy process, in this way, no matter which object is operated, it will not affect the other party.

However, if a sublist is nested in the copied list, during the copy process, the new object only copies a pointer to the corresponding sublist, in this case, the value in the sublist of the new object is changed.

The value in the old object also changes accordingly. That is to say, the shortest copy value copies the pointer of the sub-list, that is, the shortest copy can only copy the first layer of an object, the second and third layers cannot be copied (this is the sublist ). This idea is not only for the list, but also for other data types.

 

 

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.