numpy.ndarray.setfield#

方法

ndarray.setfield(val, dtype, offset=0)#

將值放入資料型別定義的欄位中的指定位置。

將 *val* 放入 *a* 中由 dtype 定義的欄位,並從欄位開始處偏移 *offset* 個位元組。

參數:
val物件

要放入欄位的值。

dtypedtype 物件

要放入 *val* 的欄位的資料型別。

offset整數,可選

要放入 *val* 的欄位位元組偏移量。

回傳:

另請參閱

getfield

範例

>>> import numpy as np
>>> x = np.eye(3)
>>> x.getfield(np.float64)
array([[1.,  0.,  0.],
       [0.,  1.,  0.],
       [0.,  0.,  1.]])
>>> x.setfield(3, np.int32)
>>> x.getfield(np.int32)
array([[3, 3, 3],
       [3, 3, 3],
       [3, 3, 3]], dtype=int32)
>>> x
array([[1.0e+000, 1.5e-323, 1.5e-323],
       [1.5e-323, 1.0e+000, 1.5e-323],
       [1.5e-323, 1.5e-323, 1.0e+000]])
>>> x.setfield(np.eye(3), np.int32)
>>> x
array([[1.,  0.,  0.],
       [0.,  1.,  0.],
       [0.,  0.,  1.]])