Python3 deque (two-way Queue), python3deque queue

Source: Internet
Author: User

Python3 deque (two-way Queue), python3deque queue

Create two-way queue

import collectionsd = collections.deque()

Append (add an element to the right)

Import collectionsd = collections. deque () d. append (1) d. append (2) print (d) # output: deque ([1, 2])

Appendleft (add an element to the left)

Import collectionsd = collections. deque () d. append (1) d. appendleft (2) print (d) # output: deque ([2, 1])

Clear (clear Queue)

Import collectionsd = collections. deque () d. append (1) d. clear () print (d) # output: deque ([])

Copy)

Import collectionsd = collections. deque () d. append (1) new_d = d. copy () print (new_d) # output: deque ([1])

Count (returns the number of occurrences of the specified element)

Import collectionsd = collections. deque () d. append (1) d. append (1) print (d. count (1) # output: 2

Extend (extend a list element from the right of the queue)

Import collectionsd = collections. deque () d. append (1) d. extend ([3, 4, 5]) print (d) # output: deque ([1, 3, 4, 5])

Extendleft (extend a list element from the left of the queue)

Import collectionsd = collections. deque () d. append (1) d. extendleft ([3, 4, 5]) print (d) ### output: deque ([5, 4, 3, 1])

Index (search for the index location of an element)

Import collectionsd = collections. deque () d. extend (['A', 'B', 'C', 'D', 'E']) print (d. index ('E') print (d. index ('C',) # specify the search interval # output: deque (['A', 'B', 'C', 'D', 'E']) #4 #2

Insert (insert an element at a specified position)

Import collectionsd = collections. deque () d. extend (['A', 'B', 'C', 'D', 'E']) d. insert (2, 'z') print (d) # output: deque (['A', 'B', 'z', 'C', 'D ', 'E'])

Pop (obtain the rightmost element and delete it in the queue)

Import collectionsd = collections. deque () d. extend (['A', 'B', 'C', 'D', 'E']) x = d. pop () print (x, d) # output: e deque (['A', 'B', 'C', 'D'])

Popleft (get the leftmost element and delete it in the queue)

Import collectionsd = collections. deque () d. extend (['A', 'B', 'C', 'D', 'E']) x = d. popleft () print (x, d) # output: a deque (['B', 'C', 'D', 'E'])

Remove (delete a specified element)

Import collectionsd = collections. deque () d. extend (['A', 'B', 'C', 'D', 'E']) d. remove ('C') print (d) # output: deque (['A', 'B', 'D', 'E'])

Reverse (queue reversal)

Import collectionsd = collections. deque () d. extend (['A', 'B', 'C', 'D', 'E']) d. reverse () print (d) # output: deque (['E', 'D', 'C', 'B', 'a'])

Rotate (place the right element to the left)

Import collectionsd = collections. deque () d. extend (['A', 'B', 'C', 'D', 'E']) d. rotate (2) # specifies the number of times. The default value is print (d) # output: deque (['D', 'E', 'A', 'B ', 'C'])

 

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.