简体   繁体   English

如何将列表附加到空的numpy数组

[英]How do I append a list to an empty numpy array

I have a functionality that for every iteration fetches elements and appends it to a list. 我有一个功能,可以在每次迭代时提取元素并将其附加到列表中。 At the end of certain number of iterations (say 1 million) I want to append the list to a numpy array, then empty the list and continue the process. 在一定数量的迭代(例如一百万次)结束时,我想将列表追加到一个numpy数组中,然后清空列表并继续该过程。

I have declared an empty numpy array as 我已声明一个空的numpy数组为

a= np.array([], dtype="int32")

b =[1,2,3,4] is my list for first 1 million iteration, 

b =[5,4,3,2] is the list for second 1 million iteration

how do I keep on appending the list b to the numpy array a after every 1 million iteration. 每100万次迭代后,如何继续将列表b追加到numpy数组a。

I need an output as given below 我需要如下输出

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

I have tried "concatenate" and "vstack" , but the problem is the dimension for a(when empty) and b doesn't match, so the code gives error. 我已经尝试过“ concatenate”和“ vstack”,但是问题是a(当为空)和b的尺寸不匹配,因此代码给出了错误。

The list is going to be as big as 1 million, so need an cost effective method to deal with the append. 该列表将多达100万,因此需要一种经济高效的方法来处理附加内容。 I could also "vstack" elements for each iteration, but that will load the huge list every time I vstack, which would not be cost-efficient. 我还可以为每个迭代“添加vstack”元素,但是每次我使用vstack时都会加载庞大的列表,这不会节省成本。 I tried the below code, which is working just fine but I want to avoid the check at every iteration. 我尝试了下面的代码,它工作正常,但我想避免在每次迭代时进行检查。

if any(a):
    a=np.vstack((a,b))
else:
    a=np.append(a,b, axis=0)

Is there any way I can append a list to a numpy array without performing the check. 有什么办法可以在不执行检查的情况下将列表追加到numpy数组。

I would recommend not appending to the array as that can be very inefficient. 我建议不要追加到数组,因为这样可能会非常低效。 Instead, you could use deque for collecting the lists, and make an array from that only when you need it. 相反,您可以使用deque收集列表,并仅在需要时才使用该deque列数组。 Here is an example: 这是一个例子:

from collections import deque
import numpy as np

lists = deque()
for i in range(1, 13, 4):
     lists.append(range(i, i + 4))

result = np.array(lists)

Now we have 现在我们有

>>> result
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])

deque is a linked list, which means that we don't have to reallocate memory for the whole container once new elements appear. deque是一个链表,这意味着一旦出现新元素,我们就不必为整个容器重新分配内存。

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

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