Python3 deque (bidirectional queue)

Source: Internet
Author: User
Tags shallow copy

Create a two-way queue

Import= Collections.deque ()

Append (add an element to the right)

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

Appendleft (add an element to the left)

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

Clear (Empty queue)

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

Copy (shallow copy)

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

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

Import= collections.deque () d.append (1) d.append (1)Print (D.count (1))# output: 2

Extend (extends the element of a list from the right side of the queue)

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

Extendleft (extends the element of a list from the left side of the queue)

Import= collections.deque () d.append (1) d.extendleft ([3,4,5]) Print (d) ## #输出: Deque ([5, 4, 3, 1])

Index (Find the index position of an element)

ImportCOLLECTIONSD=Collections.deque () d.extend (['a','b','C','D','e'])Print(d)Print(D.index ('e'))Print(D.index ('C', 0, 3))#Specify a search interval#output: deque ([' A ', ' B ', ' C ', ' d ', ' e '])#4#   2

Insert (Inserts an element at the specified location)

ImportCOLLECTIONSD=Collections.deque () d.extend (['a','b','C','D','e']) D.insert (2,'Z')Print(d)#output: deque ([' A ', ' B ', ' Z ', ' C ', ' d ', ' e '])

Pop (Gets the rightmost element and deletes it in the queue)

ImportCOLLECTIONSD=Collections.deque () d.extend (['a','b','C','D','e']) x=D.pop ()Print(x,d)#output: E deque ([' A ', ' B ', ' C ', ' d '])

Popleft (Gets the leftmost element and deletes it in the queue)

ImportCOLLECTIONSD=Collections.deque () d.extend (['a','b','C','D','e']) x=D.popleft ()Print(x,d)#output: A deque ([' B ', ' C ', ' d ', ' e '])

Remove (removes the specified element)

ImportCOLLECTIONSD=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 (Put the right element on the left)

ImportCOLLECTIONSD=Collections.deque () d.extend (['a','b','C','D','e']) d.rotate (2)#specified number of times, default 1Print(d)#output: Deque ([' d ', ' e ', ' A ', ' B ', ' C ')

Python3 deque (bidirectional queue)

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.