程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Copy() in Python numpy (copy)

編輯:Python


Not replicated (No Copy at All)

Specific explanation :

import numpy as np
a = np.arange(12) #a For a sequence
b = a # No new objects have been created
print('a Of shape by :', a.shape) # Output a The size of the
print('b yes a Do you ?', b is a) #ab Two names for the same object
b.shape = 3, 4 # take b Of shape change
print('a Of shape Turn into :', a.shape) #a Of shanpe And it changed

Output results :

a Of shape by : (12,)
b yes a Do you ? True
a Of shape Turn into : (3, 4)

View or shallow copy (View or Shallow Copy)

Different array objects can type the same data ,view Method to create a new object that is the same as the original array .

Specific explanation

a = np.arange(12)
c = a.view() # Build a and a Same c
print('c When not changed a Of shape by :', a.shape) # Output a The size of the
print('c yes a Do you ?', c is a)
print('c In order to a Based on it ', c.base is a)
c.shape = 3, 4
print('c After change a Of shape by :', a.shape)

Output results :

c When not changed a Of shape by : (12,)
c yes a Do you ? False
c In order to a Based on it True
c After change a Of shape by : (12,)

Deep copy (Deep Copy)

This is the time d yes a Copy , Just a simple copy , There's nothing to do with the two :

Specific explanation

a = np.arange(12)
d = a.copy() # Build a and a Same c
print('d yes a Do you ?', d is a)
print('d In order to a Based on it ', d.base is a)

Output results :

d yes a Do you ? False
d In order to a Based on it False

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved