问题
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