0

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)
aabceh
  • 95
  • 7

1 Answers1

1

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')
Corralien
  • 109,409
  • 8
  • 28
  • 52
  • This almost works, but since the S dtype is for a zero terminated string, if the value ends with \x00 it will get stripped off. I need the values in the array to be exactly the same as in the initial list. – aabceh Jan 30 '23 at 00:04