.I have a 2D array of strings and I intend to concatenate the strings along each row with a space between them. I used the following code, but it fails with a simple input, but it shouldn't be the case.
import numpy as np
a = np.array([['3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
'3','3', '3', '3', '3', '9', '11', '3', '3'],
['3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
'11', '9', '11', '3', '3', '3', '3', '3', '3']])
def concat(x, separator):
return separator.join(x).strip()
b = np.apply_along_axis(concat, -1, a, ' ')
Expected:
b = ['3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 9 11 3 3',
'3 3 3 3 3 3 3 3 3 3 3 11 9 11 3 3 3 3 3 3']
Output (See the end of the 2nd row of b):
b = ['3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 9 11 3 3',
'3 3 3 3 3 3 3 3 3 3 3 11 9 11 3 3 3 3 3 ']
The last 3
is missing in the output. Any reason why this should happen?