将NumPy数组附加到NumPy数组

我有一个numpy_array。有点像[ a b c ]。


然后我想将其附加到另一个NumPy数组中(就像我们创建列表列表一样)。我们如何创建包含NumPy数组的NumPy数组的数组?


我试图做以下没有任何运气


>>> M = np.array([])

>>> M

array([], dtype=float64)

>>> M.append(a,axis=0)

Traceback (most recent call last):

 File "<stdin>", line 1, in <module>

AttributeError: 'numpy.ndarray' object has no attribute 'append'

>>> a

array([1, 2, 3])


杨__羊羊
浏览 1202回答 3
3回答

呼啦一阵风

In [1]: import numpy as npIn [2]: a = np.array([[1, 2, 3], [4, 5, 6]])In [3]: b = np.array([[9, 8, 7], [6, 5, 4]])In [4]: np.concatenate((a, b))Out[4]:&nbsp;array([[1, 2, 3],&nbsp; &nbsp; &nbsp; &nbsp;[4, 5, 6],&nbsp; &nbsp; &nbsp; &nbsp;[9, 8, 7],&nbsp; &nbsp; &nbsp; &nbsp;[6, 5, 4]])或这个:In [1]: a = np.array([1, 2, 3])In [2]: b = np.array([4, 5, 6])In [3]: np.vstack((a, b))Out[3]:&nbsp;array([[1, 2, 3],&nbsp; &nbsp; &nbsp; &nbsp;[4, 5, 6]])

慕盖茨4494581

Sven说了这一切,只是非常谨慎,因为在调用append时会自动进行类型调整。In [2]: import numpy as npIn [3]: a = np.array([1,2,3])In [4]: b = np.array([1.,2.,3.])In [5]: c = np.array(['a','b','c'])In [6]: np.append(a,b)Out[6]: array([ 1.,&nbsp; 2.,&nbsp; 3.,&nbsp; 1.,&nbsp; 2.,&nbsp; 3.])In [7]: a.dtypeOut[7]: dtype('int64')In [8]: np.append(a,c)Out[8]:&nbsp;array(['1', '2', '3', 'a', 'b', 'c'],&nbsp;&nbsp; &nbsp; &nbsp; dtype='|S1')如您所见,dtype从int64到float32,然后到S1
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python