numpy.recarray.byteswap#
method
- recarray.byteswap(inplace=False)#
交换数组元素的字节
通过返回一个字节交换数组,在低位优先和高位优先数据表示之间切换,可以选择就地交换.字节字符串数组不进行交换.复数的实部和虚部会分别进行交换.
- 参数:
- inplacebool, 可选
如果为
True,则就地交换字节,默认为False.
- 返回:
- outndarray
字节交换后的数组.如果 inplace 为
True,则这是 self 的一个视图.
示例
>>> import numpy as np >>> A = np.array([1, 256, 8755], dtype=np.int16) >>> list(map(hex, A)) ['0x1', '0x100', '0x2233'] >>> A.byteswap(inplace=True) array([ 256, 1, 13090], dtype=int16) >>> list(map(hex, A)) ['0x100', '0x1', '0x3322']
字节字符串数组不进行交换
>>> A = np.array([b'ceg', b'fac']) >>> A.byteswap() array([b'ceg', b'fac'], dtype='|S3')
A.view(A.dtype.newbyteorder()).byteswap()生成一个数组,该数组具有相同的值但在内存中具有不同的表示形式.>>> A = np.array([1, 2, 3],dtype=np.int64) >>> A.view(np.uint8) array([1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0], dtype=uint8) >>> A.view(A.dtype.newbyteorder()).byteswap(inplace=True) array([1, 2, 3], dtype='>i8') >>> A.view(np.uint8) array([0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3], dtype=uint8)