📜  Numpy ndarray.setfield()函数| Python

📅  最后修改于: 2022-05-13 01:55:25.449000             🧑  作者: Mango

Numpy ndarray.setfield()函数| Python

numpy.ndarray.setfield()函数将值放入由数据类型定义的字段中的指定位置。将 val 放入由 dtype 定义的字段中,并将开始偏移字节放入该字段中。

代码#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]]