本文介绍了初始化未知长度的数组numpy的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望能够为构建上飞一个numpy的数组,我不知道这个数组的大小提前。
I want to be able to 'build' a numpy array on the fly, I do not know the size of this array in advance.
例如我想要做这样的事情:
For example I want to do something like this:
a= np.array()
for x in y:
a.append(x)
这将导致x的包含所有要素,显然这是一个简单的答案。我只是好奇,这是否是可能的?
Which would result in a containing all the elements of x, obviously this is a trivial answer. I am just curious whether this is possible?
推荐答案
建立一个Python列表,将其转换为numpy的数组。这需要每追加+ O摊销O(1)时间( N 的)为转换到阵列,总共为O( N 的)。
Build a Python list and convert that to a Numpy array. That takes amortized O(1) time per append + O(n) for the conversion to array, for a total of O(n).
a = []
for x in y:
a.append(x)
a = np.array(a)
这篇关于初始化未知长度的数组numpy的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!