我想用循环缓冲区(x为deque)
来做这个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)
但我不能用它来重现第一个代码。
答案 0 :(得分:2)
这样的事情有用吗?
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
一些提示: