我如何在numpy数组中有一个空数组

时间:2014-10-22 18:02:33

标签: python arrays numpy

我想知道我怎么能有这样的事情:array( [ [], [], [], [1, 0], [1], [3, 2], [4, 5], [5] ] ) 与numpy数组?

我尝试连接,追加,vstack但没有任何工作,我总是以这条消息结束:

ValueError: all the input array dimensions except for the concatenation axis must match exactly

2 个答案:

答案 0 :(得分:1)

只需使用np.append()但请注意,您需要将列表中的索引传递为[[1], [1, 2], []]

>>> a= np.array([[1], [1, 2], []], dtype=object)
>>> np.append(a,np.array([[],[22]]))
array([[1], [1, 2], [], [], [22]], dtype=object)

答案 1 :(得分:0)

>>> import numpy
>>> a = numpy.array( [ [], [], [], [1, 0], [1], [3, 2], [4, 5], [5] ])
>>> a
array([[], [], [], [1, 0], [1], [3, 2], [4, 5], [5]], dtype=object)
>>>
>>> numpy.hstack([a,[1], [1, 2], []])
array([[], [], [], [1, 0], [1], [3, 2], [4, 5], [5], 1, 1, 2], dtype=object)
>>> numpy.hstack([a,[[1], [1, 2], []]])
array([[], [], [], [1, 0], [1], [3, 2], [4, 5], [5], [1], [1, 2], []], dtype=object)
>>> np.append(a,np.array([[],[22]]))
array([[1], [1, 2], [], [], [22]], dtype=object)

也许我不明白你在问什么

相关问题