Numpy concatenate, using * or similar

我是研究僧i 提交于 2021-01-28 13:31:22

问题


I have a list of numpy arrays. Something like this (it won't be the same example, but similar)

lst = [np.array([ 1,2,3,4,5,6 ]).reshape(-1, 1), np.array([ 1,2,3,4,5,6 ]).reshape(-1, 1), np.array([ 1,2,3,4,5,6 ]).reshape(-1, 1)]

My lst in this case has 3 numpy arrays where their shape is (6,1), now I'd like to concatenate it, in something like this:

# array([[1, 1, 1],
#        [2, 2, 2],
#        [3, 3, 3],
#        [4, 4, 4],
#        [5, 5, 5],
#        [6, 6, 6]])

and this works perfectly doing this...

example = np.c_[lst[0], lst[1], lst[2]]

but my lst is not always the same size, so I tried this.

example = np.c_[*lst]

but it doesn't work. Is there any way to concatenete a whole list in this way?


回答1:


You can use column_stack function:

import numpy as np

lst = [np.array([1, 2, 3, 4, 5, 6]).reshape(-1, 1), np.array([1, 2, 3, 4, 5, 6]).reshape(-1, 1), np.array([1, 2, 3, 4, 5, 6]).reshape(-1, 1)]

example = np.column_stack(lst)
print(example)
[[1 1 1]
 [2 2 2]
 [3 3 3]
 [4 4 4]
 [5 5 5]
 [6 6 6]]


来源:https://stackoverflow.com/questions/62109123/numpy-concatenate-using-or-similar

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