Create Numpy array from list of arbitrary sized bites

59 views Asked by At

How can I create a numpy array from a python list of bytes objects of an arbitrary (but known) size?

Example:

size = 10
byte_list = [np.random.default_rng().bytes(size) for i in range(100)]

numpy_array = # make array from byte_list

# do something with the array
test_vals = np.random.default_rng().choice(numpy_array, size=10)

I tried to do something like this, but got an error that it didn't understand 'B10' as a data type.

numpy_array = np.fromiter(byte_list, dtype=np.dtype(f'B{size}'), count=100)
1

There are 1 answers

1
Corralien On

I think you should use S dtype and not B

numpy_array = np.fromiter(byte_list, dtype=np.dtype(f'S{size}'), count=100)
#                                              HERE --^     
# Unsigned byte (only one)
>>> np.dtype('B')
dtype('uint8')

# Byte string
>>> np.dtype('S10')
dtype('S10')