I have a huge array (of arrays) of integers in the range 0-255. Since I know the range of the integers, hence I want to optimize the space occupied by them by storing each integer within a single byte.
In C++, I would simply use char to store the integers, but I am not able to find a way out in Python.
>>> a = 10
>>> sys.getsizeof(a)
24
>>> b = chr(a)
>>> sys.getsizeof(b)
38
>>> c = bytearray(1)
>>> c[0] = b
>>> c[0]
10
>>> sys.getsizeof(c[0])
24
>>> c
bytearray(b'\n')
>>> sys.getsizeof(c)
50
I have searched for data types available in Python, but I am not able to get any data type which can give me sys.getsizeof() equal to 1.
I want to know whether there exists a spatially optimal way of storing such integers.
arraymodule. But if you also want fast operations on your data, you might as well use Numpy.