gpt4 book ai didi

python - 使用空格分隔符连接多个 numpy 字符串数组

转载 作者:行者123 更新时间:2023-12-05 00:45:31 28 4
gpt4 key购买 nike

有没有办法概括Element-wise string concatenation in 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??

期望:

array(['a d g', 'b e h', 'c f i'], dtype='<U5')

最佳答案

试试 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')

关于python - 使用空格分隔符连接多个 numpy 字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63332200/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com