Python LIFO list/array - 移动数据,用最新值替换第一个输入

Python LIFO list/array - shifting data, to replace first input with newest value

我的目标是找到价格数据集中的最高点。但是,我目前正在努力在我的 for 循环中循环遍历大量数据,以 LIFO 顺序(后进先出)将数据附加到列表中。例如: 我有一个清单 [] 在 for 循环中逐项附加到列表:list [1, 2, 3, 4, 5] 然后我达到了所需的列表长度(在本例中为 5),我想在删除 '1' 的同时向下移动所有内容,例如它可以转到 [2, 3, 4, 5, 1] 然后用 '6' 替换 '1' 导致 [2, 3, 4, 5, 6]

highs_list = np.array([])
    list_length = 50

for current in range(1, len(df.index)):
        previous = current - 1

        if len(highs_list) < list_length:
            np.append(highs_list, df.loc[current, 'high'])
        else:
            np.roll(highs_list, -1)
            highs_list[49] = df.loc[current, 'high']

如果你插入 1, 2, 3, 4, 5 然后想移除 1 以插入 6 那么这似乎是一个 FIFO 移动,因为 First IN (1) 是 First OUT。

无论如何,标准 Python 列表通过使用 append()pop(0) 允许这样做,但是元素在内存中的移动具有时间复杂度 O(n)。

完成此任务的一个更有效的工具是 collections.deque,它还提供了一个 rotate 方法来允许 [1,2,3,4,5] => [2,3,4,5,1] 转换。