Python Basics: Detailed use of deque (for manipulating elements at both ends of the list)

Source: Internet
Author: User
Tags shallow copy

Create a 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) #输出: Deque ([1, 2])

Appendleft (add an element to the left)

Import COLLECTIONSD = Collections.deque () d.append (1) d.appendleft (2) print (d) #输出: Deque ([2, 1])

Clear (Empty queue)

Import COLLECTIONSD = Collections.deque () d.append (1) d.clear () print (d) #输出: Deque ([])

Copy (shallow copy)

Import COLLECTIONSD = Collections.deque () d.append (1) new_d = D.copy () print (new_d) #输出: 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)) #输出: 2

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

Import COLLECTIONSD = Collections.deque () d.append (1) d.extend ([3,4,5]) print (d) #输出: Deque ([1, 3, 4, 5])

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

Import COLLECTIONSD = 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)

Import COLLECTIONSD = Collections.deque () d.extend ([' A ', ' B ', ' C ', ' d ', ' e ']) print (d) Print (D.index (' e ')) Print (D.index ( ' C ', 0,3))  #指定查找区间 # Output: deque ([' A ', ' B ', ' C ', ' d ', ' E ']) #     4#     2

Insert (Inserts an element at the specified location)

Import COLLECTIONSD = Collections.deque () d.extend ([' A ', ' B ', ' C ', ' d ', ' e ']) D.insert (2, ' Z ') print (d) #输出: deque ([' A ', ' B ') , ' Z ', ' C ', ' d ', ' e '])

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

Import COLLECTIONSD = Collections.deque () d.extend ([' A ', ' B ', ' C ', ' d ', ' e ']) x = D.pop () print (x,d) #输出: E deque ([' A ', ' B ', ' C ', ' d '])

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

Import COLLECTIONSD = Collections.deque () d.extend ([' A ', ' B ', ' C ', ' d ', ' e ']) x = D.popleft () print (x,d) #输出: A deque ([' B ', ' C ', ' d ', ' e '])

Remove (removes the specified element)

Import COLLECTIONSD = Collections.deque () d.extend ([' A ', ' B ', ' C ', ' d ', ' e ']) d.remove (' C ') print (d) #输出: deque ([' A ', ' B ', ' d ', ' e '])

Reverse (queue reversal)

Import COLLECTIONSD = Collections.deque () d.extend ([' A ', ' B ', ' C ', ' d ', ' e ']) D.reverse () print (d) #输出: deque ([' E ', ' d ', ' C ' , ' B ', ' a '])

Rotate (Put the right element on the left)

Import COLLECTIONSD = Collections.deque () d.extend ([' A ', ' B ', ' C ', ' d ', ' e ']) d.rotate (2)   #指定次数, default 1 times print (d) # Output: Deque ([' d ', ' e ', ' A ', ' B ', ' C ')

Python Basics: Detailed use of deque (for manipulating elements at both ends of the list)

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.