保存最后N个元素
cookbook系列
问题:对要搜索的值的最后几项做个有限的历史记录。
方案:
1 #coding=utf-8 2 from collections import deque 3 4 def search(lines, pattern, history=5): 5 previous_lines = deque(maxlen=history) #deque双端队列 6 for line in lines: 7 if pattern in line: 8 yield line, previous_lines #生成器,双端队列保存5个,意味着在找到pattern之前会记录5行数据,pattern就在line里面 9 previous_lines.append(line) 10 11 # Example use on a file 12 if __name__ == '__main__': 13 with open('somefile.txt') as f: 14 for line, prevlines in search(f, 'python', 5): 15 for pline in prevlines: 16 print pline, #先输出队列里的5个 17 print line, #在输出pattern的line 18 print('-'*20)
案例文件:somefile.txt

=== Keeping the Last N Items ==== Problem You want to keep a limited history of the last few items seen during iteration or during some other kind of processing. ==== Solution Keeping a limited history is a perfect use for a `collections.deque`. For example, the following code performs a simple text match on a sequence of lines and prints the matching line along with the previous N lines of context when found: [source,python] ---- from collections import deque def search(lines, pattern, history=5): previous_lines = deque(maxlen=history) for line in lines: if pattern in line: for pline in previous_lines: print(lline, end='') print(line, end='') print() previous_lines.append(line) # Example use on a file if __name__ == '__main__': with open('somefile.txt') as f: search(f, 'python', 5) ---- ==== Discussion Using `deque(maxlen=N)` creates a fixed size queue. When new items are added and the queue is full, the oldest item is automatically removed. For example: [source,pycon] ---- >>> q = deque(maxlen=3) >>> q.append(1) >>> q.append(2) >>> q.append(3) >>> q deque([1, 2, 3], maxlen=3) >>> q.append(4) >>> q deque([2, 3, 4], maxlen=3) >>> q.append(5) >>> q deque([3, 4, 5], maxlen=3) ---- Although you could manually perform such operations on a list (e.g., appending, deleting, etc.), the queue solution is far more elegant and runs a lot faster. More generally, a `deque` can be used whenever you need a simple queue structure. If you don't give it a maximum size, you get an unbounded queue that lets you append and pop items on either end. For example: [source,pycon] ---- >>> q = deque() >>> q.append(1) >>> q.append(2) >>> q.append(3) >>> q deque([1, 2, 3]) >>> q.appendleft(4) >>> q deque([4, 1, 2, 3]) >>> q.pop() 3 >>> q deque([4, 1, 2]) >>> q.popleft() 4 ---- Adding or popping items from either end of a queue has O(1) complexity. This is unlike a list where inserting or removing items from the front of the list is O(N).
运行结果:

H:\Python27_64\python.exe H:/myfile/python-cookbook-master/src/1/keeping_the_last_n_items/example.py Keeping a limited history is a perfect use for a `collections.deque`. For example, the following code performs a simple text match on a sequence of lines and prints the matching line along with the previous N lines of context when found: [source,python] -------------------- previous_lines.append(line) # Example use on a file if __name__ == '__main__': with open('somefile.txt') as f: search(f, 'python', 5) -------------------- 进程已结束,退出代码0
讨论:
第5行双端队列deque的用法
固定长度队列:
>>> q = deque(maxlen=3) >>> q.append(1) >>> q.append(2) >>> q.append(3) >>> q deque([1, 2, 3], maxlen=3) >>> q.append(4) >>> q deque([2, 3, 4], maxlen=3) >>> q.append(5) >>> q deque([3, 4, 5], maxlen=3) 双端队列: >>> q = deque() >>> q.append(1) >>> q.append(2) >>> q.append(3) >>> q deque([1, 2, 3]) >>> q.appendleft(4) >>> q deque([4, 1, 2, 3]) >>> q.pop() 3 >>> q deque([4, 1, 2]) >>> q.popleft() 4
两者皆有:设置好固定长度时,左右两端均可添加和删除
使用双端队列要比列表 方便、简洁!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!