Concatenate 3D numpy arrays by row

删除回忆录丶 提交于 2021-02-05 08:35:40

问题


I have the following 2 3D numpy arrays that I want to concatenate. The arrays look like this:

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"]]])

I want to concatenate the two arrays to become one 3D array like:

[[['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']]]

How do I do this?


回答1:


Use np.hstack:

np.hstack([a, b])

Output:

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


来源:https://stackoverflow.com/questions/57141802/concatenate-3d-numpy-arrays-by-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!