Python in a shallow copy of the list of deep copy of the alternative understanding of the way

Source: Internet
Author: User
Tags shallow copy

‘‘‘
Created by: Xu Yang
Created: June 13, 2017 22:25:23
Code Features: Detailed analysis list of shallow copy and deep copy
‘‘‘
#导入copy模块
Import Copy
List1 = [1,2,3,["A", "B", "C"]]
#对表1进行浅复制
List2 = Copy.deepcopy (list1) #copy. Copy
#查询表1中各个元素的内存地址
Address0 = ID (list1[0])
Address1 = ID (list1[1])
Address2 = ID (list1[2])
ADDRESS3 = ID (list1[3])
ADDRESS30 = ID (list1[3][0])
ADDRESS31 = ID (list1[3][1])
Address32 = ID (list1[3][2])
#查询表2中各个元素的内存地址
Address_0 = ID (list2[0])
Address_1 = ID (list2[1])
address_2 = ID (list2[2])
Address_3 = ID (list2[3])
address_30 = ID (list2[3][0])
address_31 = ID (list2[3][1])
address_32 = ID (list2[3][2])
# Print out the memory address of table 1 and the memory address of List 2
Print ("---------first comparison---------")
Print ("Listing 1:", ADDRESS0,ADDRESS1,ADDRESS2,ADDRESS3,ADDRESS30,ADDRESS31,ADDRESS32)
Print ("Listing 2:", address_0,address_1,address_2,address_3,address_30,address_31,address_32)
# Modify a value in List 1
List1[1]=-1
#查询表1中各个元素的内存地址
Address0 = ID (list1[0])
Address1 = ID (list1[1])
Address2 = ID (list1[2])
ADDRESS3 = ID (list1[3])
ADDRESS30 = ID (list1[3][0])
ADDRESS31 = ID (list1[3][1])
Address32 = ID (list1[3][2])
#查询表2中各个元素的内存地址
Address_0 = ID (list2[0])
Address_1 = ID (list2[1])
address_2 = ID (list2[2])
Address_3 = ID (list2[3])
address_30 = ID (list2[3][0])
address_31 = ID (list2[3][1])
address_32 = ID (list2[3][2])
# Print the memory address of this list 1 and the memory address of List 2
Print ("---------second comparison---------")
Print ("Listing 1:", ADDRESS0,ADDRESS1,ADDRESS2,ADDRESS3,ADDRESS30,ADDRESS31,ADDRESS32)
Print ("Listing 2:", address_0,address_1,address_2,address_3,address_30,address_31,address_32)
Print (LIST1,LIST2)
# Modify the value in the third element in list 1
LIST1[3][1] = 6
#查询表1中各个元素的内存地址
Address0 = ID (list1[0])
Address1 = ID (list1[1])
Address2 = ID (list1[2])
ADDRESS3 = ID (list1[3])
ADDRESS30 = ID (list1[3][0])
ADDRESS31 = ID (list1[3][1])
Address32 = ID (list1[3][2])
#查询表2中各个元素的内存地址
Address_0 = ID (list2[0])
Address_1 = ID (list2[1])
address_2 = ID (list2[2])
Address_3 = ID (list2[3])
address_30 = ID (list2[3][0])
address_31 = ID (list2[3][1])
address_32 = ID (list2[3][2])
# Print the memory address of this list 1 and the memory address of List 2
Print ("---------third comparison---------")
Print ("Listing 1:", ADDRESS0,ADDRESS1,ADDRESS2,ADDRESS3,ADDRESS30,ADDRESS31,ADDRESS32)
Print ("Listing 2:", address_0,address_1,address_2,address_3,address_30,address_31,address_32)
# cause Analysis: Look at the picture
Print (LIST1,LIST2)

Python in a shallow copy of the list of deep copy of the alternative understanding of the way

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.