This section provides some other ways to use deque .
Provides a feature similar to the command tail in UNIX that displays a text at the end of a file:
def tail (filename, n=10):
' returns the last n lines of text ' of a file
with open (filename) as F:
Return deque (f, N)
For example, use deque to calculate a moving average:
def moving_average (Iterable, n=3):
# moving_average ([Max, Max, Max, 40.0 42.0 45.0 43.0
# Http://en.wikipedia.org/wiki/Moving_average
it = iter (iterable)
D = deque (Itertools.islice (it, n-1))
D.appendleft (0)
s = SUM (d)
For Elem in it:
s + = Elem-d.popleft ()
D.append (Elem)
Yield S/n
Moving average ( moving average-ma) is also called the moving average price, is the use of the principle of statistical moving average, the past a certain number of days of securities, futures transaction price ( weighted ) average, coherent line of price. And according to its order, multiplication, crossing, break down and other phenomena, to study the point of entering the appearance.
Use rotate () to delete the element at the specified location:
def delete_nth (d, N):
D.rotate (-N)
D.popleft ()
D.rotate (N)
Cai Junsheng qq:9073204 Shenzhen
5.3.3.1 deque Other ways to use