numpy.recarray.getfield#

method

recarray.getfield(dtype, offset=0)#

将给定数组的字段作为某种类型返回.

字段是具有给定数据类型的数组数据的视图.视图中的值由给定类型和到当前数组中的字节偏移量决定.偏移量需要使得视图 dtype 适合数组 dtype;例如,dtype 为 complex128 的数组具有 16 字节的元素.如果使用 32 位整数(4 字节)获取视图,则偏移量需要在 0 到 12 字节之间.

参数:
dtypestr 或 dtype

视图的数据类型.视图的 dtype 大小不能大于数组本身.

offsetint

在开始元素视图之前要跳过的字节数.

示例

>>> import numpy as np
>>> x = np.diag([1.+1.j]*2)
>>> x[1, 1] = 2 + 4.j
>>> x
array([[1.+1.j,  0.+0.j],
       [0.+0.j,  2.+4.j]])
>>> x.getfield(np.float64)
array([[1.,  0.],
       [0.,  2.]])

通过选择 8 字节的偏移量,我们可以为我们的视图选择数组的复数部分:

>>> x.getfield(np.float64, offset=8)
array([[1.,  0.],
       [0.,  4.]])