[SOLVED] Python: String concatenation working differently inside for loop

Issue

I have a NumPy array containing a list which contains strings of various lengths:

arr = np.array(["abcd", "abcdef", "ab"])

I’m trying to loop through the strings in order to pad them to a constant length. If i do it one at a time, like so:

new_len = 10

left_pad = divmod(new_len - len(arr[0]),2)[0]
right_pad = left_pad + divmod(new_len - len(arr[0]),2)[1]

abcd = arr[0].join(["_"*left_pad, "_"*right_pad])

I get my desired output of:

'___abcd___'

But if I try doing it in a loop, like so:

for i in range(arr.shape[0]):
    left_pad = divmod(new_len - len(arr[i]),2)[0]
    right_pad = left_pad + divmod(new_len - len(arr[i]),2)[1]
    arr[i] = arr[i].join(["_"*left_pad, "_"*right_pad])

I get this different output:

array(['___abc', '__abcd', '____ab'], dtype='<U6')

I’d like to understand why the behaviour is different in these two cases, and how I can get the desired output with a loop. Thanks in advance for any help or suggestions.

Solution

Try to define your array as an array of objects like in the example bellow:

arr = np.array(["abcd", "abcdef", "ab"], dtype='object')

According to the output of your example you’ve created an array of char with length of 6 (dtype='<U6′)

Answered By – daniboy000

Answer Checked By – Timothy Miller (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *