當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python Numpy ndarray.getfield()用法及代碼示例

numpy.ndarray.getfield()函數以給定類型返回給定數組的字段。

用法: numpy.ndarray.getfield(dtype, offset=0)

參數:
dtype :[str或dtype]視圖的dtype大小不能大於數組本身的大小。
offset:[int]開始元素視圖之前要跳過的字節數。



Return :以給定類型返回給定數組的字段。

代碼1:

# Python program explaining 
# numpy.ndarray.getfield() function 
  
# importing numpy as geek  
import numpy as geek  
            
x = geek.diag([1.+1.j]*2) 
            
gfg = x.getfield(geek.float64) 
      
print(gfg)

輸出:

[[ 1.  0.]
 [ 0.  1.]]


代碼2:

# Python program explaining 
# numpy.ndarray.getfield() function 
  
# importing numpy as geek  
import numpy as geek  
            
x = geek.diag([2.+2.j]*2) 
            
gfg = x.getfield(geek.float64, offset = 8) 
      
print(gfg)

輸出:

[[ 2.  0.]
 [ 0.  2.]]



相關用法


注:本文由純淨天空篩選整理自sanjoy_62大神的英文原創作品 Numpy ndarray.getfield() function | Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。