numpy.recarray.getfield#
方法
- 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.]])