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个元素-LMLPHP

使用双端队列要比列表 方便、简洁!

05-23 15:14