Element-wise string concatenation in numpy

后端 未结 5 786
萌比男神i
萌比男神i 2020-11-30 04:04

Is this a bug?

import numpy as np
a1=np.array([\'a\',\'b\'])
a2=np.array([\'E\',\'F\'])

In [20]: add(a1,a2)
Out[20]: NotImplemented

I am t

5条回答
  •  独厮守ぢ
    2020-11-30 04:22

    Another solution is to convert string arrays into arrays of python of objects so that str.add is called:

    >>> import numpy as np
    >>> a = np.array(['a', 'b', 'c', 'd'], dtype=np.object)   
    >>> print a+a
    array(['aa', 'bb', 'cc', 'dd'], dtype=object)
    

    This is not that slow (less than twice as slow as adding integer arrays).

提交回复
热议问题