19 lines
513 B
Python
19 lines
513 B
Python
# 队列可以对最后N个元素进行保存
|
|
from collections import deque
|
|
|
|
# 从历史记录列表history得到length条最新历史记录
|
|
def latest_history(history, length):
|
|
window = deque(maxlen=length)
|
|
for record in history:
|
|
window.append(record)
|
|
|
|
# window引用双头队列组件deque,该组件有方法如下
|
|
# 从队尾加入
|
|
window.append(1)
|
|
# 从队尾弹出
|
|
window.pop()
|
|
# 从队头加入
|
|
window.appendleft(1)
|
|
# 从队头弹出
|
|
window.popleft()
|
|
|