循环缓冲区python

Circular buffer python

我想用一个循环缓冲区(即 x 作为双端队列)来做到这一点

i = 0
x = []

while True:
    accel_data = sensor.get_accel()
    d = datetime.utcnow().strftime('%Y-%m-%d')
    t = datetime.utcnow().strftime('%H:%M:%S.%f')
    x.append(accel_data + (d, t))
    i = i + 1

我知道如何实现一个简单的循环缓冲区:

from collections import deque
import time

d = deque(maxlen=4)
bool = True
i = 1
y = 0
while bool:
    d.append(i)
    i = i + 1
    print(d)
    time.sleep(1) 

但是我不能用它来重现第一个代码。

这样的东西行得通吗?

from collections import deque

container = deque(maxlen=4)
while True:
    accel_data = sensor.get_accel()
    curr_date = datetime.utcnow().strftime('%Y-%m-%d')
    curr_time = datetime.utcnow().strftime('%H:%M:%S.%f')
    entry = accel_data + (curr_date, curr_time)
    container.append(entry)
    print(container)  # this is not strictly necessary

一些提示:

  1. 为您正在使用的变量使用合理的名称。
  2. 不要初始化/声明您不打算使用的变量。
  3. 更具体地说明您没有管理的内容。