Python numpy.ndarray.byteswap()
numpy.ndarray.byteswap()函数通过返回一个字节交换的数组在低endian和big-endian数据表示之间进行切换,可以选择就地交换。
语法: ndarray.byteswap(inplace=False)
参数:
inplace: [bool, optional] 如果为真,就地交换字节,默认为假。
返回值:
out :[ndarray] 字节交换的数组。如果inplace是True,这就是一个对自己的视图。
代码 #1:
# Python program explaining
# byteswap() function
import numpy as geek
# a is an array of integers.
a = geek.array([1, 256, 100], dtype = np.int16)
print(a.byteswap(True))
输出 :
[256 1 25600]
代码 #2:
byteswap()函数对字符串数组不起作用。
# Python program explaining
# byteswap() function
import numpy as geek
# a is an array of strings
a = geek.array(["arka","soumen","simran"],dtype = np.int16)
print(a.byteswap(True))
输出 :
ValueError Traceback (most recent call last)
in ()
1 import numpy as geek
----> 2 a = geek.array(["arka","soumen","simran"],dtype = np.int16)
3
4 #a is an array of strings
5
ValueError: invalid literal for int() with base 10: 'arka'