numpy.ndarray.getfield#
方法
- ndarray.getfield(dtype, offset=0)#
以特定型別回傳給定陣列的欄位。
欄位是以給定資料型別檢視陣列資料的方式。檢視中的值由給定型別和當前陣列的位元組偏移量決定。偏移量需要確保檢視 dtype 符合陣列 dtype;例如,dtype 為 complex128 的陣列具有 16 位元組的元素。如果使用 32 位元整數(4 位元組)進行檢視,則偏移量需要在 0 到 12 位元組之間。
- 參數:
- dtypestr 或 dtype
檢視的資料型別。檢視的 dtype 大小不能大於陣列本身。
- offset整數
在開始元素檢視之前要跳過的位元組數。
範例
>>> 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.]])