2025-09-10:仓库迁移
This commit is contained in:
19
1.数据结构与算法/3.保存最后N个元素.py
Normal file
19
1.数据结构与算法/3.保存最后N个元素.py
Normal file
@@ -0,0 +1,19 @@
|
||||
# 队列可以对最后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()
|
||||
|
Reference in New Issue
Block a user