-
Notifications
You must be signed in to change notification settings - Fork 0
/
deep_copy_nested_list.py
47 lines (32 loc) · 1.04 KB
/
deep_copy_nested_list.py
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
data_0 = [1,2]
data_1 = [3,4]
data_2D = [data_0,data_1,10]
data_2D_copy = data_2D.copy()
print(f"Data = {data_2D}")
print(f"Data Copy = {data_2D_copy}")
# mengambil data dari nested list
data = data_2D[1][0]
print(f"Data = {data}")
# address semuanya
print(f"address asli = {hex(id(data_2D))}")
print(f"address data_2D = {hex(id(data_2D_copy))}")
print("Address dari member ke-1")
print(f"address asli = {hex(id(data_2D[0]))}")
print(f"address data_2D = {hex(id(data_2D_copy[0]))}")
data_2D[1][0] = 5
data_2D[2] = 9
print(f"Data = {data_2D}")
print(f"Data copy = {data_2D_copy}")
# menggunakan deep copy
from copy import deepcopy
data_2D = [data_0,data_1,10]
data_2D_deepcopy = deepcopy(data_2D)
print(f"address asli = {hex(id(data_2D))}")
print(f"address deep = {hex(id(data_2D_deepcopy))}")
print("Address dari member ke-1")
print(f"address asli = {hex(id(data_2D[0]))}")
print(f"address copy = {hex(id(data_2D_deepcopy[0]))}")
data_2D[1][0] = 30
print(f"Data = {data_2D}")
print(f"Data Copy = {data_2D_copy}")
print(f"Data deep = {data_2D_deepcopy}")