For the purpose of my application, I can declare an array of strings in two ways:
As a list
strArr1 = [""] * 5
orAs a numpy array
strArr2 = numpy.empty([5], dtype=str)
However, I see the following difference when I try to concatenate characters to array elements. In the first case, e.g.
strArr1[0] += 'a'
strArr1[0] += 'b'
gives me as expected ['ab', '', '', '', '']
.
In the second case however,
strArr2[0] += 'a'
strArr2[0] += 'b'
gives me the result ['a', '', '', '', '']
.
Why is concatenation not working as expected for the elements of numpy array? Also, given that I have the constraint that I must extend the elements of my array one character at a time, could anyone suggest an efficient and pythonic approach?
Thanks.