0% found this document useful (0 votes)
2 views8 pages

Deep Copy View Copy 20250515

The document discusses different types of copying in Python, including no copy, shallow copy, and deep copy. It illustrates these concepts using examples with lists and NumPy arrays, highlighting how changes to the original data affect the copies. The document also explains the behavior of views and independent copies in the context of NumPy arrays.

Uploaded by

bfb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views8 pages

Deep Copy View Copy 20250515

The document discusses different types of copying in Python, including no copy, shallow copy, and deep copy. It illustrates these concepts using examples with lists and NumPy arrays, highlighting how changes to the original data affect the copies. The document also explains the behavior of views and independent copies in the context of NumPy arrays.

Uploaded by

bfb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

List

no copy, shallow copy,


deep copy
No copy
• a=[0,1,2,3]
• b=a
• print(b)
• b[1]=10
• print(b)
• print(a)
Shallow Copy
Shallow Copy for 2-D List
Deep Copy
np.array
view and copy
import numpy as np
a=[[1 2]
[3 4]
a=np.array([[1,2],[3,4],[5,6]])
[5 6]]
print(f"a={a}")
a.base=None
print(f"a.base={a.base}")
b=[[1 2 3]
b=a.reshape((2,3))
[4 5 6]]
print(f"b={b}")
b.base=[[1 2]
print(f"b.base={b.base}") b 為 a 的 view ,與 a 不是獨立
[3 4]
的陣列
[5 6]]
c=a[1:3]
print(f"c={c}")
c=[[3 4]
print(f"c.base={c.base}")
[5 6]]
c.base=[[1 2]
[3 4]
b2=a.reshape((2,3)).copy() 是 copy 過來的陣列
[5 6]]
print(f"b2={b2}")
print(f"b2.base={b2.base}")
b2=[[1 2 3]
[4 5 6]]
b2.base=None
c2=a[1:3].copy()
print(f"c2={c2}")
c2=[[3 4]
print(f"c2.base={c2.base}")
[5 6]]
c2.base=None
a[1,1]=40
a=[[ 1 2]
print(f"a={a}") [ 3 40]
[ 5 6]]
print(f"b={b}")
b=[[ 1 2 3]
print(f"c={c}") [40 5 6]]

print(f"b2={b2}") c=[[ 3 40]


[ 5 6]]
print(f"c2={c2}")
b2=[[1 2 3]
[4 5 6]]

c2=[[3 4]
[5 6]]

You might also like