=== 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).
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Python CookBook(3rd)_Code 源码 (365个子文件)
access-log 636KB
access-log 636KB
data.bin 256B
access-log-0108.bz2 45KB
access-log-0208.bz2 45KB
pysample.c 2KB
sample.c 2KB
embed.c 2KB
pysample.c 2KB
sample.c 2KB
pysample.c 2KB
pysample.c 2KB
sample.c 1KB
sample.c 1KB
ptexample.c 1KB
sample.c 1KB
sample.c 979B
sample.c 689B
stocks.csv 309B
sample.dat 27B
20121220.log.gz 145KB
20121218.log.gz 141KB
20121217.log.gz 136KB
20121219.log.gz 135KB
20121221.log.gz 134KB
20121227.log.gz 128KB
20121228.log.gz 125KB
20121226.log.gz 124KB
20121225.log.gz 119KB
20121230.log.gz 119KB
20121229.log.gz 117KB
20121224.log.gz 116KB
20121223.log.gz 112KB
20121222.log.gz 110KB
access-log-0208.gz 71KB
access-log-0108.gz 71KB
pysample.h 730B
sample.h 279B
sample.i 1KB
config.ini 516B
logconfig.ini 333B
Makefile 108B
Makefile 70B
README.md 138B
csample.pxd 335B
urlimport.py 7KB
netsched.py 5KB
example.py 5KB
example_clsdec.py 4KB
example4.py 4KB
example.py 4KB
example1.py 3KB
example.py 3KB
example3.py 3KB
example3.py 3KB
daemon.py 3KB
example.py 2KB
sample.py 2KB
example1.py 2KB
namelower.py 2KB
threadpool.py 2KB
example.py 2KB
example1.py 2KB
example2.py 2KB
example2.py 2KB
example2.py 2KB
tcpserver.py 2KB
hardexample.py 2KB
example.py 2KB
example1.py 2KB
pqueue.py 2KB
actorsched.py 2KB
example1.py 2KB
example.py 2KB
example2.py 1KB
plyexample.py 1KB
actor.py 1KB
example2.py 1KB
ssl_xmlrpc_server.py 1KB
example2.py 1KB
example.py 1KB
example2.py 1KB
example.py 1KB
example.py 1KB
rpcserver.py 1KB
jsonrpcserver.py 1KB
example2.py 1KB
example1.py 1KB
example2.py 1KB
server1.py 1KB
example1.py 1KB
example2.py 1KB
exchange2.py 1KB
example1.py 1KB
example.py 1KB
example.py 1KB
example.py 1KB
example2.py 1KB
example.py 1KB
example2.py 1KB
共 365 条
- 1
- 2
- 3
- 4
资源评论
- fenh2019-02-21不错~~~~~~~~~~
- 迹二2018-04-15不错~~~~~~~
yinkaisheng-nj
- 粉丝: 763
- 资源: 6953
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功