简体   繁体   English

如何通过添加 1d numpy ZA3CCBC3F9D0CE2D71D1555 从空的 numpy 数组制作 2d numpy 数组

[英]How to make a 2d numpy array from an empty numpy array by adding 1d numpy arrays?

So I'm trying to start an empty numpy array with a = np.array([]) , but when i append other numpy arrays (like [1, 2, 3, 4, 5, 6, 7, 8] and [9, 10, 11, 12, 13, 14, 15, 16] to this array, then the result im basically getting is So I'm trying to start an empty numpy array with a = np.array([]) , but when i append other numpy arrays (like [1, 2, 3, 4, 5, 6, 7, 8] and [9, 10, 11, 12, 13, 14, 15, 16]到这个数组,那么我基本上得到的结果是
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] . [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]

But what i want as result is: [[1, 2, 3, 4, 5, 6, 7, 8], [9, 10, 11, 12, 13, 14, 15, 16]]但我想要的结果是: [[1, 2, 3, 4, 5, 6, 7, 8], [9, 10, 11, 12, 13, 14, 15, 16]]

IIUC you want to keep adding lists to your np.array. IIUC 你想继续向你的 np.array 添加列表。 In that case, you can use something like np.vstack to "append" the new lists to the array.在这种情况下,您可以使用类似np.vstack的东西将新列表“附加”到数组中。

a = np.array([[1, 2, 3],[4, 5, 6]])
np.vstack([a, [7, 8, 9]])

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

You can also use np.c_[] , especially if a and b are already 1D arrays (but it also works with lists):您也可以使用np.c_[] ,特别是如果ab已经是一维 arrays (但它也适用于列表):

a = [1, 2, 3, 4, 5, 6, 7, 8]
b = [9, 10, 11, 12, 13, 14, 15, 16]

>>> np.c_[a, b]
array([[ 1,  9],
       [ 2, 10],
       [ 3, 11],
       [ 4, 12],
       [ 5, 13],
       [ 6, 14],
       [ 7, 15],
       [ 8, 16]])

It also works "multiple times":它也可以“多次”工作:

>>> np.c_[np.c_[a, b], a, b]
array([[ 1,  9,  1,  9],
       [ 2, 10,  2, 10],
       [ 3, 11,  3, 11],
       [ 4, 12,  4, 12],
       [ 5, 13,  5, 13],
       [ 6, 14,  6, 14],
       [ 7, 15,  7, 15],
       [ 8, 16,  8, 16]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM