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'])