python中矩阵的逻辑乘法

nun*_*usa 2 python arrays numpy

我有一个矩阵A和一个向量B,其中矩阵A01填充,向量B用字符串填充。我想执行以下操作:

A = np.array([[1,1,0],[0,1,1],[0,0,1]])
B = np.array(['a','b','c'])
Run Code Online (Sandbox Code Playgroud)

结果必须是:

R = np.array(['a'+'b', 'b'+'c', 'c'])
Run Code Online (Sandbox Code Playgroud)

有可能做到numpy吗?

Bre*_*lla 5

如果您使用以下方式定义数组b,则有一种方法dtype = object

b = np.array(['a', 'b', 'c'], dtype=object)
Run Code Online (Sandbox Code Playgroud)

那么它只是一个dot产品:

a.dot(b)
#array(['ab', 'bc', 'c'], dtype=object)
Run Code Online (Sandbox Code Playgroud)