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


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

numpy.ndarray.setfield()函數將值放入由數據類型定義的字段中的指定位置。將val放入dtype定義的字段中,並將起始偏移量字節放入該字段中。

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

參數:
val :[對象]放置在字段中的值。
dtype :[dtype object]放置val的字段的數據類型。
offset:[int,可選]放置val的字段中的字節數。



Return :沒有。

代碼1:

# Python program explaining 
# numpy.ndarray.setfield() function 
  
# importing numpy as geek  
import numpy as geek  
            
arr = geek.eye(4) 
            
arr.setfield(4, geek.int32) 
gfg = arr.getfield(geek.int32) 
      
print(gfg)

輸出:

[[4 4 4 4]
 [4 4 4 4]
 [4 4 4 4]
 [4 4 4 4]]


代碼2:

# Python program explaining 
# numpy.ndarray.setfield() function 
  
# importing numpy as geek  
import numpy as geek  
            
arr = geek.eye(2) 
            
arr.setfield(2, geek.int32) 
gfg = arr.getfield(geek.int32) 
      
print(gfg)

輸出:

[[2 2]
 [2 2]]



相關用法


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