This can be done using numpy.char.add. Here is an example:
>>> import numpy as np
>>> a1 = np.array(['a', 'b'])
>>> a2 = np.array(['E', 'F'])
>>> np.char.add(a1, a2)
array(['aE', 'bF'],
dtype='<U2')
(This was previously known as numpy.core.defchararray.add, and that name is still usable, but numpy.char.add is the preferred alias now.)
There are other useful string operations available for NumPy data types.
Answer from Mike T on Stack OverflowThis can be done using numpy.char.add. Here is an example:
>>> import numpy as np
>>> a1 = np.array(['a', 'b'])
>>> a2 = np.array(['E', 'F'])
>>> np.char.add(a1, a2)
array(['aE', 'bF'],
dtype='<U2')
(This was previously known as numpy.core.defchararray.add, and that name is still usable, but numpy.char.add is the preferred alias now.)
There are other useful string operations available for NumPy data types.
You can use the chararray subclass to perform array operations with strings:
a1 = np.char.array(['a', 'b'])
a2 = np.char.array(['E', 'F'])
a1 + a2
#chararray(['aE', 'bF'], dtype='|S2')
another nice example:
b = np.array([2, 4])
a1*b
#chararray(['aa', 'bbbb'], dtype='|S4')
It's not hard to do outside of numpy:
>>> import numpy as np
>>> pic = np.array([ 'H','e','l','l','o','W','o','r','l','d']).reshape(2,5)
>>> pic
array([['H', 'e', 'l', 'l', 'o'],
['W', 'o', 'r', 'l', 'd']],
dtype='|S1')
>>> '\n'.join([''.join(row) for row in pic])
'Hello\nWorld'
There is also the np.core.defchararray module which has "goodies" for working with character arrays -- However, it states that these are merely wrappers around the python builtin and standard library functions so you'll probably not get any real speedup by using them.
You had the right ideas there. Here's a vectorized NumPythonic implementation trying to go along those ideas -
# Create a separator string of the same rows as input array
separator_str = np.repeat(['\n'], pic.shape[0])[:,None]
# Concatenate these two and convert to string for final output
out = np.concatenate((pic,separator_str),axis=1).tostring()
Or a one-liner with np.column_stack -
np.column_stack((pic,np.repeat(['\n'], pic.shape[0])[:,None])).tostring()
Sample run -
In [123]: pic
Out[123]:
array([['H', 'e', 'l', 'l', 'o'],
['W', 'o', 'r', 'l', 'd']],
dtype='|S1')
In [124]: np.column_stack((pic,np.repeat(['\n'], pic.shape[0])[:,None])).tostring()
Out[124]: 'Hello\nWorld\n'
Try np.apply_along_axis
arr_list = [strings1, strings2, strings3]
arr_out = np.apply_along_axis(' '.join, 0, arr_list)
In [35]: arr_out
Out[35]: array(['a d g', 'b e h', 'c f i'], dtype='<U5')
You could use a for loop to help you achieve this:
import numpy as np
strings1 = np.array(["a", "b", "c"], dtype=np.str)
strings2 = np.array(["d", "e", "f"], dtype=np.str)
strings3 = np.array(["g", "h", "i"], dtype=np.str)
Create a list of your strings:
strings=[strings1, strings2, strings3]
Create an empty list:
list_for_new_array=[]
For loop to iterate through each array in strings, and create a list of strings containing array items separated by a space:
for string in strings:
i=""
for item in string:
i+=item+" "
list_for_new_array.append(i)
Create new array with list created in for loop:
new_array= np.array(list_for_array, dtype='<U5')
You have to convert the integer array to strings:
import numpy as np
out = np.core.defchararray.add('User', np.arange(10).astype(str))
print(out)
# ['User0' 'User1' 'User2' 'User3' 'User4' 'User5' 'User6' 'User7' 'User8'
# 'User9']
How about something like this:
np.array(['User{}'.format(i) for i in range(10)])