Python collections Deque

Source: Internet
Author: User

Collections is a high-level container class library of Python that contains common containers outside of dict, Truple.

The following describes the common deque

1. The deque is a double-ended queue that can be plugged in from both ends and can be taken from both ends of the element.

2. Deque is thread-safe and can be used to do multi-threaded shared resources, and I'm also starting to touch Duque.

>>> fromCollectionsImportdeque>>> a = [1, 2, 3, 4] Initialize the deque with a list>>> DEQ =deque (a)>>>Deqdeque ([1, 2, 3, 4]) to iterate over the queue list in deque original>>>Deq.extend (a)>>>Deqdeque ([1, 2, 3, 4, 1, 2, 3, 4]) from the right side of the queue element>>> Deq.append (5)>>>Deqdeque ([1, 2, 3, 4, 1, 2, 3, 4, 5]) input elements from the left side>>> Deq.appendleft (6)>>>Deqdeque ([6, 1, 2, 3, 4, 1, 2, 3, 4, 5]) from the right side of the team>>>Deq.pop ()5from the left side of the team>>>Deq.popleft ()6>>>Deqdeque ([1, 2, 3, 4, 1, 2, 3, 4]) rotate from left to right>>> Deq.rotate (-2)>>>Deqdeque ([3, 4, 1, 2, 3, 4, 1, 2]) rotate from right to left>>> Deq.rotate (2)>>>Deqdeque ([1, 2, 3, 4, 1, 2, 3, 4]) from file output to Deque for example, enter the last 3 lines of the file to get a queue of size 3>>> a = deque (open ('Test.txt'), 3)>>>Adeque (['5\n','6\n','7\n'], maxlen=3)

Python collections Deque

Related Article

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.