[Numpy-discussion] Concatenating string arrays

Sturla Molden sturla@molden...
Wed Mar 18 13:52:24 CDT 2009


On 3/18/2009 7:30 PM, Thomas Robitaille wrote:

> import numpy as np
> arr1 = np.array(['a','b','c'])
> arr2 = np.array(['d','e','f'])
> 
> I would like to produce a third array that would contain  
> ['ad','be','cf']. Is there an efficient way to do this? I could do  
> this element by element, but I need a faster method, as I need to do  
> this on arrays with several million elements.

 >>> arr1 = np.array(['a','b','c'])
 >>> arr2 = np.array(['d','e','f'])
 >>> arr3 = np.zeros(6, dtype='|S1')
 >>> arr3[::2] = arr1
 >>> arr3[1::2] = arr2
 >>> arr3.view(dtype='|S2')
array(['ad', 'be', 'cf'],
       dtype='|S2')

Does this help?


Sturla Molden



More information about the Numpy-discussion mailing list