保存最后N个元素
cookbook系列
问题:对要搜索的值的最后几项做个有限的历史记录。
方案:
#coding=utf-
from collections import deque def search(lines, pattern, history=):
previous_lines = deque(maxlen=history) #deque双端队列
for line in lines:
if pattern in line:
yield line, previous_lines #生成器,双端队列保存5个,意味着在找到pattern之前会记录5行数据,pattern就在line里面
previous_lines.append(line) # Example use on a file
if __name__ == '__main__':
with open('somefile.txt') as f:
for line, prevlines in search(f, 'python', ):
for pline in prevlines:
print pline, #先输出队列里的5个
print line, #在输出pattern的line
print('-'*)
案例文件: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=):
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', )
---- ==== 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=)
>>> q.append()
>>> q.append()
>>> q.append()
>>> q
deque([, , ], maxlen=)
>>> q.append()
>>> q
deque([, , ], maxlen=)
>>> q.append()
>>> q
deque([, , ], maxlen=)
---- 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()
>>> q.append()
>>> q.append()
>>> q
deque([, , ])
>>> q.appendleft()
>>> q
deque([, , , ])
>>> q.pop() >>> q
deque([, , ])
>>> q.popleft() ---- Adding or popping items from either end of a queue has O()
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//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', )
-------------------- 进程已结束,退出代码0
讨论:
第5行双端队列deque的用法
固定长度队列:
>>> q = deque(maxlen=)
>>> q.append()
>>> q.append()
>>> q.append()
>>> q
deque([, , ], maxlen=)
>>> q.append()
>>> q
deque([, , ], maxlen=)
>>> q.append()
>>> q
deque([, , ], maxlen=)
双端队列:
>>> q = deque()
>>> q.append()
>>> q.append()
>>> q.append()
>>> q
deque([, , ])
>>> q.appendleft()
>>> q
deque([, , , ])
>>> q.pop() >>> q
deque([, , ])
>>> q.popleft()
4
两者皆有:设置好固定长度时,左右两端均可添加和删除

使用双端队列要比列表 方便、简洁!
保存最后N个元素的更多相关文章
- appium 学习各种小功能总结--功能有《滑动图片、保存截图、验证元素是否存在、》---新手总结(大牛勿喷,新手互相交流)
1.首页滑动图片点击 /** * This Method for swipe Left * 大距离滑动 width/6 除数越大向左滑动距离也越大. * width:720 *height:1280 ...
- Python:读取txt中按列分布的数据,并将结果保存在Excel文件中 && 保存每一行的元素为list
import xlwt import os def write_excel(words,filename): #写入Excel的函数,words是数据,filename是文件名 wb=xlwt.Wor ...
- 【python cookbook】【数据结构与算法】3.保存最后N个元素
问题:希望在迭代或是其他形式的处理过程中对最后几项记录做一个有限的历史记录统计 解决方案:选择collections.deque. 如下的代码对一系列文本行做简单的文本匹配操作,当发现有匹配时就输出当 ...
- 给定字符串数组,用map的key保存数组中字符串元素,value保存字符串元素出现次数,最后统计个字符串元素出现次数
import java.util.HashMap; public class map1 { public static void main(String[] args) { String[] arra ...
- jQuery--Dom元素隐藏和显示原理(源码2.0.3)
对于Dom元素显示和隐藏的操作,jQuery提供了比较方便的函数,我们也经常使用: 1. show() : 显示Dom元素2. hide() : 隐藏Dom元素3. toggle() : 改变Dom元 ...
- js原生封装getClassName()方法-ie不支持getElementsByClassName,所以要自己实现获取类名为className的所有元素
<html> <head> <script type="text/javascript"> window.onload = function() ...
- 求链表的倒数第m个元素
法一: 首先遍历一遍单链表,求出整个单链表的长度n,然后将倒数第m个,转换为正数第n-m+1个,接下去遍历一次就可以得到结果. 不过这种方法需要对链表进行两次遍历,第一次遍历用于求解单链表的长度,第二 ...
- [Perl] 删除数组中重复元素
写一个小程序时候,需要去除一个数组中的重复元素,搜索了一下,找到的代码主要是两种,一种是使用grep函数,一种是转换为hash表,代码分别如下: 使用grep函数代码片段:代码: my @array ...
- POI读取Excel数据保存到数据库,并反馈给用户处理信息(导入带模板的数据)
今天遇到这么一个需求,将课程信息以Excel的形式导入数据库,并且课程编号再数据库中不能重复,也就是我们需要先读取Excel提取信息之后保存到数据库,并将处理的信息反馈给用户.于是想到了POI读取文件 ...
随机推荐
- [Java多线程]-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类)
前言:刚学习了一段机器学习,最近需要重构一个java项目,又赶过来看java.大多是线程代码,没办法,那时候总觉得多线程是个很难的部分很少用到,所以一直没下决定去啃,那些年留下的坑,总是得自己跳进去填 ...
- DLL基本知识
一.生成方式: 使用DEF文件定义导出接口或使用__declspec(dllexport)描述接口,编译链接后生成dll+lib,其中lib是导入库,里面只有对导出接口的描述,而没有具体实现. 二.链 ...
- ASP.NET Core的身份认证框架IdentityServer4--入门【转】
原文地址 Identity Server 4是IdentityServer的最新版本,它是流行的OpenID Connect和OAuth Framework for .NET,为ASP.NET Cor ...
- AJAX获取服务器文件
写一个按钮,点击后在指定的div里显示本地txt文件内容 在本地新建一个test.txt,里面随便写点内容就好. <!DOCTYPE html> <html> <head ...
- 常用Path路径
正三角形(左):<Path Data="M40,0 L0,30 40,60 z" Stretch="Uniform"/> 正三角形(上):<P ...
- cms替换主页
cms替换主页的步骤 1.先做好静态页面: 2.在D:\wamp\www\phpcms\install_package\phpcms\templates文件夹下建新的文件夹tianqiwangluo( ...
- 数组C - 玛雅日历
During his last sabbatical, professor M. A. Ya made a surprising discovery about the old Maya calend ...
- laravel artisan 工具心得
介绍一些非常好用的命令: 1.创建一个Eloquent模型:顺便创建一个对应的数据库表 php artisan make:model --migration Models/Admin/test 2.将 ...
- HDU 1072 Nightmare (广搜)
题目链接 Problem Description Ignatius had a nightmare last night. He found himself in a labyrinth with a ...
- 前端QRCode.js生成二维码(解决长字符串模块和报错问题)
QRCode 用法 1.使用npm安装到你的项目中 npm install qrcode2 --save 使用commonjs或者es6模块方式导入 var QRCode = require('qrc ...