【发布时间】:2017-02-02 00:55:11
【问题描述】:
假设我有以下np.uint8 数组:
In [9]: a = np.array([0x34, 0xF3, 0x87, 0x42]).astype(np.uint8)
In [10]: a
Out[10]: array([0x34, 0xf3, 0x87, 0x42], dtype=uint8)
现在,当我转换为 np.uint16 时,我得到以下信息:
In [11]: b = a.astype(np.uint16)
In [12]: b
Out[12]: array([0x34, 0xf3, 0x87, 0x42], dtype=uint16)
这是预期会发生的,但我想要别的。
例如,在 C 语言中,当您拥有类型为 uint8 (unsigned char) 的相同数组并且您想要访问它时,就好像它是包含 uint16 (unsigned short) 元素的数组一样:
unsigned char a[] = {0x34, 0xF3, 0x87, 0x42};
unsigned short* b = (unsigned short*)a;
这会给我,正如 C 所期望的那样:
0x34 0xF3 0x87 0x42 // for a
0xF334 0x4287 // for b, little or big endian, for me doesn't matter
现在我的问题是,我怎样才能在 Python 中做这种事情? (是否甚至可以在不创建新数组的情况下从一种类型转换为另一种类型?)
我可以通过位移和添加两个字节来创建一个新数组,如下所示:
#! /usr/bin/python3
import numpy as np
np.set_printoptions(formatter={'int':hex})
# I assume, a has the len 2*n and b has the len n (for conversion from 2*n Bytes in n 2Bytes)
a = np.array([0x31, 0x41, 0x59, 0x26, 0x53, 0x58, 0x97, 0x93]).astype(np.uint8)
b = np.array([(a[2*i]<<8)+a[2*i+1] for i in range(0, len(a) // 2)]).astype(np.uint16)
print("a: {}".format(a)) # a: [0x31 0x41 0x59 0x26 0x53 0x58 0x97 0x93]
print("b: {}".format(b)) # b: [0x3141 0x5926 0x5358 0x9793]
我需要这个用于非常大的数组(或多或少),所以我想问这是否可以在 Python 中更有效地完成。
【问题讨论】:
标签: python python-3.x numpy type-conversion