通过重新解释原始字节从一种类型的numpy数组转换为另一种

Jas*_*n S 4 python arrays numpy python-2.7

有什么办法用numpy数组做“ reinterpret_cast”吗?这是一个例子:

>>> import numpy as np
>>> x=np.array([105,79,196,53,151,176,59,202,249,0,207,6], dtype=np.uint8)
>>> np.fromstring(x.tostring(),'<h')
array([ 20329,  13764, -20329, -13765,    249,   1743], dtype=int16)
Run Code Online (Sandbox Code Playgroud)

我可以调用tostring(),然后fromstring()从一个数组转换为原始字节,然后再返回到另一个数组。我只是想知道我是否有办法跳过中间步骤。(我想了解的不是什么大不了的。)

unu*_*tbu 6

是。当您查看具有不同dtype的数组时,您将根据不同的dtype重新解释基础数据(零和一)。

In [85]: x.view('<i2')
Out[85]: array([ 20329,  13764, -20329, -13765,    249,   1743], dtype=int16)
Run Code Online (Sandbox Code Playgroud)