当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python numpy dtype.newbyteorder用法及代码示例


本文简要介绍 python 语言中 numpy.dtype.newbyteorder 的用法。

用法:

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

返回具有不同字节顺序的新 dtype。

数据类型的所有字段和子数组也会进行更改。

参数

new_order 字符串,可选

字节顺序强制;来自以下字节顺序规范的值。默认值 (‘S’) 会导致交换当前字节顺序。 new_order 代码可以是以下任何一种:

  • ‘S’ - 将 dtype 从当前交换到相反的字节序

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

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

  • {‘=’, ‘native’} - 原生订单

  • {‘|’, ‘I’} - 忽略(不改变字节顺序)

返回

new_dtype 类型

具有给定字节顺序更改的新 dtype 对象。

注意

数据类型的所有字段和子数组也会进行更改。

例子

>>> import sys
>>> sys_is_le = sys.byteorder == 'little'
>>> native_code = sys_is_le and '<' or '>'
>>> swapped_code = sys_is_le and '>' or '<'
>>> 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

相关用法


注:本文由纯净天空筛选整理自numpy.org大神的英文原创作品 numpy.dtype.newbyteorder。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。