Python deep copy and python copy

Source: Internet
Author: User

Python deep copy and python copy

Python data structures are divided into two types:

1. String and number

2. List, tuples, and dictionaries

I. Strings and numbers

For strings and numbers, assigning values (=), copying, and deepcopy is meaningless because they always point to the same memory address.

>>> Import copy >>> a1 = 100 >>> id (a1) 1426656816 # a1 memory address # assignment >>> a2 = a1 >>> id (a2) 1426656816 # shallow copy> a3 = copy. copy (a1) >>> id (a3) 1426656816 # Deep copy >>> a4 = copy. deepcopy (a1) >>> id (a4) 1426656816

  

 

 

 

 

Ii. List, tuples, and dictionaries

For lists and dictionaries, the changes caused by assigning values (=), copying, and deepcopy are different.

2.1 value assignment (=)
Names = ['angle ', 'zous', 'athena', ['banana', 'apple'] name2 = namesnames [1] = "Pear" print (names) print (name2) print ("-----------------") names [3] [1] = "Dog" print (names) print (name2) print (id (names )) print (id (name2) # output ['angle ', 'pear', 'athena', ['banana ', 'apple'] ['angle ', 'pear', 'athena', ['bana', 'apple'] --------------- ['angle ', 'pear', 'athena', ['bana ', 'Dog '] ['angle', 'pear ', 'athena', ['banana', 'dog '] 15043874065361504387406536 # You can see every change in names, name2 will also change because their memory addresses are the same

  

  

This is different from the string and number. When we define a = 1, B = a, and change a, B will not change. The list and dictionary will change as their memory addresses are the same.

A = 1b = a = 100 print (a) print (B) # output 1001

  

2.2. copy)
Import copynames = ['angle ', 'zous', 'athena', ['banana', 'apple'] name2 = copy. copy (names) names [1] = "Zeus" print (names, "names memory address is {}". format (id (names) print (name2, "the memory address of name2 is {}". format (id (name2) # output ['angle ', 'Zeus', 'athena', ['banana ', 'apple'] names memory address is 1764417452744 ['angle ', 'zous', 'athena', ['bana ', 'apple'] the memory address of name2 is 1764416035080. We copied a name2. At the same time, we changed the Zous value in names to Chinese, but the name of name2 has not changed. Because their memory addresses are different, changing one does not affect the other.

Next we will look:

Import copynames = ['angle ', 'zous', 'athena', ['banana', 'apple'] name2 = copy. copy (names) names [1] = "Zeus" print (names, "names memory address is {}". format (id (names) print (name2, "the memory address of name2 is {}". format (id (name2) names [3] [1] = 'apple' print (names, id (names [3]) print (name2, id (name2 [3]) # output ['angle ', 'Zeus', 'athena', ['banana ', 'apple'] the names memory address is 2306153560776 ['angle ', 'zous', 'athena', ['banana', 'apple'] name2. The storage address is 2306152155528 ['angle ', 'Zeus', 'athena', ['banana ', 'apple'] 2306153648968 ['angle', 'zous ', 'hena', ['banana ', 'apple'] 2306153648968 # This time we not only changed the value of the list on the outermost layer, but also the value of a list on the list. # The result of this operation is that the list in the layer is changed.

We can see from the printed memory address that the memory address of the entire outer list is different, but the memory address of the internal list is consistent.

  

Summary: copy. copy (x) only copies the first layer of data types such as list, and the memory address is changed. However, the memory address of the Data Type in the layer does not change.

 

2.3. Deep copy)

Deep copy (copy. deepcopy (x) is actually re-opening a new memory address to store the data after deepcopy, which is completely different from the memory address of the original data, including the memory address of the layer data type.

Import copynames = ['angle ', 'zous', 'athena', ['banana', 'apple'] name2 = copy. deepcopy (names) names [1] = "Zeus" print (names, "names memory address is {}". format (id (names) print (name2, "the memory address of name2 is {}". format (id (name2) names [3] [1] = 'apple' print (names, id (names [3]) print (name2, id (name2 [3]) # output ['angle ', 'Zeus', 'athena', ['banana ', 'apple'] names memory address is 2379824216776 ['angle ', 'zous', 'athena', ['bana', 'apple'] name The memory address of 2 is 2379824217160 ['angle ', 'use', 'athena', ['banana', 'apple'] 2379824304968 ['angle ', 'zous ', 'hena', ['banana ', 'apple'] 2379824305032 # It can be seen that names does not affect name2.

:

  

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.