Pre-Python 4--depth copy and direct assignment

Source: Internet
Author: User
Tags shallow copy
# Direct assignment, after which the variable is applied to the same ID () address, and subsequent modifications will be used to drink also modified
num1=[1,2,3,4]
Num2=num1
Print (ID (NUM1))
Print (ID (num2))
num2[0]=111
Print (NUM1)
Print (NUM2)

#深拷贝 completely open up new space, modify the value after copy, the value will not be affected
Import Copy
num1=[1,2,3,4]
Num2=copy.deepcopy (NUM1)
Print (NUM1,NUM2)
Print (ID (NUM1))
Print (ID (num2))
num2[0]=111
Print (NUM1,NUM2)

# Shallow copy  only if the list exists in the sub-list, its child list ID () is the same, and subsequent modifications to the values in the sub-list will be modified initially
num1=[1,2,3,4,[5,6,7]]
Num2=copy.copy (NUM1)
Print (NUM1,NUM2)
Print (ID (NUM1))
Print (ID (num2))
Print (ID (num1[-1)))
Print (ID (num2[-1)))
num2[-1][0]=[8,9,0]
Print (NUM1)
Print (NUM2)

Pre-Python 4--depth copy and direct assignment

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.