Answer the question
In order to leave comments, you need to log in
How can I set the string dtype when using the apply_along_axis method, or is it better to represent an array as a string?
You need to represent the array as a string with the given element separators. Came across such behavior of the following code.
>>> def array_to_string(array, sep):
>>> return f'{sep}'.join(map(str, array))
>>>
>>>
>>> a = np.arange(12).reshape((3, 4))
>>> np.apply_along_axis(array_to_string, 1, a, ',')
array(['0,1,2,3', '4,5,6,7', '8,9,10,'], dtype='<U7')
>>> a = np.arange(16).reshape((4, 4))
>>> np.apply_along_axis(array_to_string, 1, a, ',')
array(['0,1,2,3', '4,5,6,7', '8,9,10,', '12,13,1'], dtype='<U7')
>>> a = np.arange(9).reshape((3, 3))
>>> np.apply_along_axis(array_to_string, 1, a, ',')
array(['0,1,2', '3,4,5', '6,7,8'], dtype='<U5')
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question