numpy.dtype.newbyteorder#

方法

dtype.newbyteorder(new_order='S', /)#

傳回具有不同位元組順序的新 dtype。

資料型別的所有欄位和子陣列也會進行變更。

參數:
new_orderstring, optional

強制位元組順序;來自以下位元組順序規格的值。預設值 ('S') 會導致交換目前的位元組順序。new_order 代碼可以是下列任一值

  • ‘S’ - 將 dtype 從目前位元組順序交換為相反的位元組順序

  • {‘<’, ‘little’} - 小端序

  • {‘>’, ‘big’} - 大端序

  • {‘=’, ‘native’} - 原生順序

  • {‘|’, ‘I’} - 忽略 (不變更位元組順序)

傳回值:
new_dtypedtype

具有指定位元組順序變更的新 dtype 物件。

註解

資料型別的所有欄位和子陣列也會進行變更。

範例

>>> import sys
>>> sys_is_le = sys.byteorder == 'little'
>>> native_code = '<' if sys_is_le else '>'
>>> swapped_code = '>' if sys_is_le else '<'
>>> import numpy as np
>>> native_dt = np.dtype(native_code+'i2')
>>> swapped_dt = np.dtype(swapped_code+'i2')
>>> native_dt.newbyteorder('S') == swapped_dt
True
>>> native_dt.newbyteorder() == swapped_dt
True
>>> native_dt == swapped_dt.newbyteorder('S')
True
>>> native_dt == swapped_dt.newbyteorder('=')
True
>>> native_dt == swapped_dt.newbyteorder('N')
True
>>> native_dt == native_dt.newbyteorder('|')
True
>>> np.dtype('<i2') == native_dt.newbyteorder('<')
True
>>> np.dtype('<i2') == native_dt.newbyteorder('L')
True
>>> np.dtype('>i2') == native_dt.newbyteorder('>')
True
>>> np.dtype('>i2') == native_dt.newbyteorder('B')
True