Open In App

Numpy ndarray.setfield() function | Python

numpy.ndarray.setfield() function Put a value into a specified place in a field defined by a data-type. Place val into a’s field defined by dtype and beginning offset bytes into the field.

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



Parameters :
val : [object] Value to be placed in field.
dtype : [dtype object] Data-type of the field in which to place val.
offset : [int, optional] The number of bytes into the field at which to place val.

Return : None.



Code #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)

Output :

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

 
Code #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)

Output :

[[2 2]
 [2 2]]
Article Tags :