numpy.char.chararray.getfield#

方法

char.chararray.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.]])