Numpy ndarray.setfield()函数| Python
numpy.ndarray.setfield()
函数将值放入由数据类型定义的字段中的指定位置。将 val 放入由 dtype 定义的字段中,并将开始偏移字节放入该字段中。
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.
代码#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]]