5.3.3.1 deque Other ways to use

Source: Internet
Author: User
Tags iterable

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.