To ask you, what is the copy? Copy is a transliteration of the word, in fact, he is the copy from the English transliteration of the word, what is the copy? copy is actually a copy, also known as a copy. In fact, the depth of copy is complete copy, and a copy of part of the meaning.
1, look at the assignment operator.
l1 = [1,2,3,['barry','alex']] l2 = l1 l1[0] = 111 print(l1) # [111, 2, 3, ['barry', 'alex']] print(l2) # [111, 2, 3, ['barry', 'alex']] l1[3][0] = 'wusir' print(l1) # [111, 2, 3, ['wusir', 'alex']] print(l2) # [111, 2, 3, ['wusir', 'alex']]
For the assignment operator is, l1 and l2 points to the same memory address, so they are exactly the same, in for example, such as Zhangsanlisi shared together, then for the living room, they are common, Joe Smith can be used John Doe can also be used, but suddenly one day Joe Smith replaced the TV in the living room of projection, then use the living room when John Doe, did not want to watch TV, but rather a projection, right? l1, l2 point that any change in a variable list with a list of the variables after using the rest of the list, this list is the list after the change.
2, shallow copy copy.
The same block of code #: L1 = [. 1, 'Bai', True, (l, 2,3), [22 is, 33 is]] L2 = l1.copy () Print (ID (L1), ID (L2)) 2713214468360 2713214524680 # Print (ID (L1 [-2]), ID (L2 [-2])) # 2,547,618,888,008 2,547,618,888,008 Print (ID (L1 [-1]), ID (L2 [-1])) # 2547620322952 2547620322952 # different code blocks: >>> L1 = [. 1, 'Bai', True, (. 1, 2,. 3), [22 is, 33 is]] >>> l1.copy L2 = () >>> Print (ID ( L1), ID (L2)) 1,477,183,162,120 1,477,183,162,696 >>> Print (ID (L1 [-2]), ID (L2 [-2])) 1477181814032 1477181814032 >>> Print (ID (L1 [-1]), ID (L2 [-1])) 1,477,183,162,504 1,477,183,162,504
For shallow copy, it just re-created in memory opens up a space for a new list, but the elements in the new list with elements of the original list is common.
3, deep copy deepcopy.
The same block of code # Import Copy L1 = [. 1, 'Alex', True, (l, 2,3), [22 is, 33 is]] L2 = copy.deepcopy (L1) Print (ID (L1), ID (L2 )) # 2788324482440 2,788,324,483,016 Print (ID (L1 [0]), ID (L2 [0])) # 1470562768 1470562768 Print (ID (L1 [-1]), ID (L2 [-1])) # 2788324482632 2,788,324,482,696 Print (id (l1 [-2]) , id (l2 [-2])) # 2788323047752 2788323047752 at different code blocks # >>> Import Copy >>> L1 = [. 1, 'Bai', True, (1, 2 ,. 3), [22 is, 33 is]] >>> copy.deepcopy L2 = (L1) >>> Print (ID (L1), ID (L2)) 1,477,183,162,824 1477183162632 >>> Print (ID (0), ID ( 0)) 1470562736 1470562736 >>> Print (ID (-2), ID (-2)) 1470562672 1470562672 >>>print(id(l1[-1]), id(l2[-1])) 1477183162120 1477183162312
For deep copy, the list is re-created in memory, the data type of the variable list is re-created, immutable data types in the list are shared.
l1 = [1, 2, 3, 4, ['alex']] l2 = l1[::] l1[-1].append(666) print(l2)