wei*_*i14 4 python arrays string numpy
有没有办法将numpy 中的按元素字符串连接推广到 n > 2 种情况,并使用空格分隔符执行连接 " "。该np.char.add函数仅适用于 2 个数组,并且没有添加分隔符的选项。
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)
# Concatenate several string dtype arrays with a space delimiter
# I.e. something like strings1 + " " + strings2 + " " + strings3
# Code??
Run Code Online (Sandbox Code Playgroud)
期望:
array(['a d g', 'b e h', 'c f i'], dtype='<U5')
Run Code Online (Sandbox Code Playgroud)
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')
Run Code Online (Sandbox Code Playgroud)