简体   繁体   English

如何合并Python列表中包含的数组

[英]How to combine arrays which are contained in a list in python

If I have something like; 如果我有类似的东西;

a=np.array([1,2])
b=np.array([1,4])
c=[]

Firstly, what is the built in python function to enable the combination of a and b so that we have array([1,2],[1,4]) 首先,什么是内置的python函数来实现a和b的组合,以便我们拥有array([1,2],[1,4])

Also another question just for future reference, in the past ended up with things like [array([1,2]), array([1,4])] I wasn't sure what to do so I just had to redo the code. 还有另一个问题,仅供将来参考,过去以[array([1,2]), array([1,4])]类的东西结束,我不确定该怎么做,所以我只需要重做码。 Is it bad practise to end up with a result like this or is there a function which can change this to array([1,2],[1,4]) ? 以这样的结果结尾是不好的做法,还是有一个函数可以将其更改为array([1,2],[1,4])

Thank you. 谢谢。

See numpy vstack function: 请参见numpy vstack函数:

>>> import numpy as np
>>> a = np.array([1, 2])
>>> b = np.array([3, 4])
>>> np.vstack((a, b))
array([[1, 2],
       [3, 4]])

You might want to look into np.concatenate 您可能需要研究np.concatenate

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

http://docs.scipy.org/doc/numpy/reference/generated/numpy.concatenate.html http://docs.scipy.org/doc/numpy/reference/generation/numpy.concatenate.html

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

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