gpt4 book ai didi

python - 按行连接 3D numpy 数组

转载 作者:行者123 更新时间:2023-12-01 00:43:53 28 4
gpt4 key购买 nike

我有以下 2 个 3D numpy 数组想要连接。数组看起来像这样:

a = np.array([[[1,1,1], 
[2,2,2],
[3,3,3]],
[["a","a","a"],
["b","b","b"],
["c","c","c"]]])

b = np.array([[[4,4,4],
[5,5,5],
[6,6,6],
[7,7,7],
[8,8,8],
[9,9,9]],
[["d","d","d"],
["e","e","e"],
["f","f","f"],
["g","g","g"],
["h","h","h"],
["i","i","i"]]])

我想将两个数组连接成一个 3D 数组,如下所示:

[[['1' '1' '1']
['2' '2' '2']
['3' '3' '3']
['4' '4' '4']
['5' '5' '5']
['6' '6' '6']
['7' '7' '7']
['8' '8' '8']
['9' '9' '9']]

[['a' 'a' 'a']
['b' 'b' 'b']
['c' 'c' 'c']
['d' 'd' 'd']
['e' 'e' 'e']
['f' 'f' 'f']
['g' 'g' 'g']
['h' 'h' 'h']
['i' 'i' 'i']]]

我该怎么做?

最佳答案

使用np.hstack :

np.hstack([a, b])

输出:

array([[['1', '1', '1'],
['2', '2', '2'],
['3', '3', '3'],
['4', '4', '4'],
['5', '5', '5'],
['6', '6', '6'],
['7', '7', '7'],
['8', '8', '8'],
['9', '9', '9']],

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

关于python - 按行连接 3D numpy 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57141802/

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