我试图将numpyint32数组看作int8类型。在
>>> a = np.array([1, 2, 3, 4], dtype='int32')
>>> a
array([1, 2, 3, 4], dtype=int32)
>>> a.view('int8')
array([1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0], dtype=int8)
我希望将1转换为[0,0,0,1],但为什么它会变成[1,0,0,0]?这与数字在内存中的存储方式有关吗?在
谢谢。在
Tags:
是的,有big endian and low endian。引用维基百科:
但是,您可以使用^{} and ^{} in the dtype 来决定要哪一个:
如果没有
<
或>
,它将使用系统的endianness。使用另一个可能会导致(轻微)性能下降。在确切地说,假设一个低位字节排序,那么每个32个数字的最低有效字节将排在第一位,最高有效字节将排在最后。所以,
DEADBEEF
(十六进制)将变成EF BE AD DE
。在相关问题 更多 >
编程相关推荐