1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
A = [1,2]
B = A[:]
print(B)
print(B is A)
A = [[1,2,3],[4,5,6]]
B = A[:]
print(A)
print(B)
B[1][2] = 100
print(B)
print(A)
print(A is B)
print(A[0] is B[0])
print(A[1] is B[1])
# 输出如下所示:
"""
[1, 2]
False
[[1, 2, 3], [4, 5, 6]]
[[1, 2, 3], [4, 5, 6]]
[[1, 2, 3], [4, 5, 100]]
[[1, 2, 3], [4, 5, 100]]
False
True
True
"""
[[1, 2, 3], [4, 5, 100]]
[[1, 2, 3], [4, 5, 6]]
[[1, 2, 3], [4, 5, 100]]
# We call B a shallow copy of A, to make a deep copy,
# we need to use the copy.deepcopy function
import copy
A = [[1,2,3],[4,5,6]]
B = copy.copy(A)
B[1][2] = 100
print(A) # same as [:]
A = [[1,2,3],[4,5,6]]
B = copy.deepcopy(A)
B[1][2] = 100
print(A)
print(B)
# 输出如下所示:
"""
[[1, 2, 3], [4, 5, 100]]
[[1, 2, 3], [4, 5, 6]]
[[1, 2, 3], [4, 5, 100]]
"""
|