numpy.matrix.byteswap#
方法
- matrix.byteswap(inplace=False)#
- 交换数组元素的字节顺序 - 通过返回一个字节序交换的数组,或选择性地原地交换,可在小端序和大端序数据表示之间切换。字节字符串数组不进行交换。复数的实部和虚部会单独交换。 - 参数:
- inplace布尔型,可选
- 如果为 - True,则原地交换字节,默认为- False。
 
- 返回:
- outndarray
- 字节序交换后的数组。如果 inplace 为 - True,则这是自身的一个视图。
 
 - 示例 - >>> 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)