python - 按行连接 3D numpy 数组

标签 python arrays numpy concatenation row

我有以下 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/

相关文章:

python - 在 Python 中清理 HTML

python - Python中如何保证一段代码一次只能被一个请求执行?

c++ - 如何更高效地遍历存储 {int, short, ushort,...} 的字符数组?

python - Einsum 优化基本操作失败

python - caffe 可以同时接收一批不同分辨率的输入吗?如果是这样怎么办?

python - Python 中的 VB.Net 子程序

javascript - 我的功能不断刷新

java - 字符串数组不匹配

python 、 Pandas : Using isin() like functionality but do not ignore duplicates in input list

python - 使用opencv可视化24位原始RGB图像数据