numpy.frombuffer()函数– Python
numpy.frombuffer()函数将缓冲区解释为一维数组。
Syntax : numpy.frombuffer(buffer, dtype = float, count = -1, offset = 0)
Parameters :
buffer : [buffer_like] An object that exposes the buffer interface.
dtype : [data-type, optional] Data-type of the returned array, default data-type is float.
count : [int, optional] Number of items to read.
offset : [int, optional] Start reading the buffer from this offset, default is 0.
Return : This function interpret a buffer as a 1-dimensional array.
代码#1:
Python3
# Python program explaining
# numpy.frombuffer() function
# importing numpy as geek
import numpy as geek
gfg = geek.frombuffer(b'\x01\x02\x03', dtype = geek.uint8)
print (gfg)
Python3
# Python program explaining
# numpy.frombuffer() function
# importing numpy as geek
import numpy as geek
gfg = geek.frombuffer(b'\x01\x02\x03\x04\x05\x06\x07', dtype = geek.uint8, count = 5)
print (gfg)
输出 :
[1 2 3]
代码#2:
Python3
# Python program explaining
# numpy.frombuffer() function
# importing numpy as geek
import numpy as geek
gfg = geek.frombuffer(b'\x01\x02\x03\x04\x05\x06\x07', dtype = geek.uint8, count = 5)
print (gfg)
输出 :
[1 2 3 4 5]