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