在一个数组中连接多个numpy数组?

HAO*_*HEN 5 arrays numpy

假设我有很多numpy数组:

a = ([1,2,3,4,5])
b = ([2,3,4,5,6])
c = ([3,4,5,6,7])
Run Code Online (Sandbox Code Playgroud)

我想生成一个新的二维数组:

d = ([[1,2,3,4,5],[2,3,4,5,6],[3,4,5,6,7]])
Run Code Online (Sandbox Code Playgroud)

我应该编码什么?我尝试过使用:

d = np.concatenate((a,b),axis=0)
d = np.concatenate((d,c),axis=0)
Run Code Online (Sandbox Code Playgroud)

它返回:

d = ([1,2,3,4,5,2,3,4,5,6,3,4,5,6,7])
Run Code Online (Sandbox Code Playgroud)

MSe*_*ert 9

如评论中所述,您可以只使用以下np.array功能:

>>> import numpy as np
>>> a = ([1,2,3,4,5])
>>> b = ([2,3,4,5,6])
>>> c = ([3,4,5,6,7])

>>> np.array([a, b, c])
array([[1, 2, 3, 4, 5],
       [2, 3, 4, 5, 6],
       [3, 4, 5, 6, 7]])
Run Code Online (Sandbox Code Playgroud)

在通常情况下,您想基于“尚不存在”的维度进行堆叠,还可以使用np.stack

>>> np.stack([a, b, c], axis=0)
array([[1, 2, 3, 4, 5],
       [2, 3, 4, 5, 6],
       [3, 4, 5, 6, 7]])

>>> np.stack([a, b, c], axis=1)  # not what you want, this is only to show what is possible
array([[1, 2, 3],
       [2, 3, 4],
       [3, 4, 5],
       [4, 5, 6],
       [5, 6, 7]])
Run Code Online (Sandbox Code Playgroud)