具有固定长度但连续填充新值的Python数组

具有固定长度但连续填充新值的Python数组,python,numpy,queue,deque,Python,Numpy,Queue,Deque,让我们假设我们有一个变量(shape(1,N,a,b,c))来存储具有shape(a,b,c)的numpy数组。我首先想用零初始化这个变量 import numpy as np N = 5 a = 20 b = 40 c = 4 storage = np.zeros(1, N, a, b, c) # collect new arrays while True: values = np.random.random((a, b, c)) # np.array with shape (a,

让我们假设我们有一个变量(shape(1,N,a,b,c))来存储具有shape(a,b,c)的numpy数组。我首先想用零初始化这个变量

import numpy as np
N = 5
a = 20
b = 40
c = 4
storage = np.zeros(1, N, a, b, c)
# collect new arrays
while True:
    values = np.random.random((a, b, c))  # np.array with shape (a, b, c)
    save_values_to_storage(values)
函数save_values_to_storage(values)的目标是填充存储器中的值。在第一个循环中,存储器的(1,N,…)值将被
值填充。在下一个循环(1,N,…)中,存储器的值将被
值填充
,之前的值将被移动到(1,N-1,…)。等等如果第一个存储的
到达第一个位置(1,1,…),并且检索并存储了新的
,则第一个
将被丢弃,以便新的
存储在位置(1,N,…),所有其他值将按其位置减少

我不知道我怎么能做出这样的行为。有点像 numpy数组的队列。因此,我的问题是如何实现 功能
将值保存到存储(值)


Edit:似乎
deque
与之类似。但是我不知道如何将它们用于numpy阵列

我想我解决了这个问题。我不确定这是否是最好的方法。我感谢任何改进

import numpy as np
import random

N = 5
a = 20
b = 40
c = 4
inputs = np.zeros((N, a, b, c))
for i in range(0, 10):
    # replace right hand side with method that gets values
    values = np.random.random((a, b, c))
    # expand the dimension to fit to inputs
    values = np.expand_dims(values, axis=0)
    # delete values in first entry of inputs
    inputs = np.delete(inputs, obj=0, axis=0)
    # concatenate inputs and values to a new array
    inputs = np.concatenate([inputs, values], axis=0)
    # for debugging
    # print('shape: {}'.format(inputs.shape))

# expand the dimension with an additional first dimension to achieve (1, N, a, b, c)  # shape.
inputs = np.expand_dims(inputs, axis = 0)

我想我解决了这个问题。我不确定这是否是最好的方法。我感谢任何改进

import numpy as np
import random

N = 5
a = 20
b = 40
c = 4
inputs = np.zeros((N, a, b, c))
for i in range(0, 10):
    # replace right hand side with method that gets values
    values = np.random.random((a, b, c))
    # expand the dimension to fit to inputs
    values = np.expand_dims(values, axis=0)
    # delete values in first entry of inputs
    inputs = np.delete(inputs, obj=0, axis=0)
    # concatenate inputs and values to a new array
    inputs = np.concatenate([inputs, values], axis=0)
    # for debugging
    # print('shape: {}'.format(inputs.shape))

# expand the dimension with an additional first dimension to achieve (1, N, a, b, c)  # shape.
inputs = np.expand_dims(inputs, axis = 0)
你查过了吗?我认为这是相关的,即使你的数据比那个问题中的维度更多。你检查过了吗?我认为这是相关的,即使你的数据比这个问题有更多的维度。