About Python's shades of Copy

Source: Internet
Author: User
Tags shallow copy

About Python's shades of Copy

1 ImportCopy2 3 #A shallow copy copies only the first layer, and the data in the back layer changes because the pointer follows the change.4Copyone = ["Walk", 28, [4300, 5000]]5CP1 =copyone.copy ()6Cp1[0] ='On_foot'7CP1[1] =' in'8Cp1[2][0] = 93009 Print("list Copyone:%s"%copyone)#after a shallow copy, 4300 of the original data has become 9300Ten Print("shallow copy effect:%s"%CP1) One  A #deep copy is full copy -Copyone = ["Walk", 28, [4300, 5000]] -App =copy.deepcopy (Copyone) theCp2[0] ='On_foot' -CP2[1] =' in' -Cp2[2][0] = 9300 - Print("list Copyone:%s"%copyone)#no changes to the original list data after deep copy + Print("shallow copy effect:%s"%CP2)

The following results are performed:

List copyone: [' Walk ', 28, [9300, 5000]]
Shallow copy effect: [' on_foot ', ' 29 ', [9300, 5000]]
List copyone: [' Walk ', 28, [4300, 5000]]
Shallow copy effect: [' on_foot ', ' 29 ', [9300, 5000]]

A deep copy may not be as clear as that. But I know it already.

About Python's shades of Copy

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.