How can I compact the dtype (which is effectively about memory usage) in a numpy string array, where the maximum string length, say U5, is shorter than that is defined in the dtype attribute, say U10.
One way to "compact" the dtype is to explicitly cast it to U5, but can this be done automatically by some function calls without mannually inspecting the string length?
For example:
>>> import numpy as np
>>> a = np.array([['a', 'bb', 'cc'], ['aaabc', 'ccc', 'b']], dtype='U10')
# Non-existing pseudo function
>>> a = compact(a)
>>> print(a.dtype)
dtype('<U5')
so the function compact compacts the redundant U10 to U5.
Thanks in advance!