Deque is an abbreviation for the double-ended queue, similar to list, but provides an operation to insert and delete at both ends.
- Appendleft inserts to the left of the list
- Popleft the value to the left of the popup list
- Extendleft extension on the left
For example:
Queue = Deque () # Append values to wait for Processingqueue.appendleft ("first") Queue.appendleft ("second") Queue.appendleft ("Third") # Pop values when Readyprocess (Queue.pop ()) # would process ' first ' # Add values while Processingq Ueue.appendleft ("Fourth") # What does the queue look like Now?queue # deque ([' Fourth ', ' third ', ' second '])
As a double-ended queue, Deque also provides some other useful methods, such as rotate, let's take a look at the following:
Fill
Deque can be populated from either end, in Python implementations called "left" and "right".
Import COLLECTIONSD1 = Collections.deque () d1.extend (' ABCDEFG ') print ' Extend: ', d1d1.append (' h ') print ' append: ', d1d2 = Collections.deque () D2.extendleft (xrange (6)) print ' Extendleft ', D2d2.appendleft (6) print ' Appendleft ', D2
Extendleft () iterates over its inputs, completing the same processing as appendleft () for each element.
Extend:deque ([' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' G ']) append:deque ([' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ']) Extendleft deque ( [5, 4, 3, 2, 1, 0] Appendleft Deque ([6, 5, 4, 3, 2, 1, 0])
Use
The deque element can be leveraged from both ends, depending on the algorithm applied.
Import Collectionsprint "From the right:" D = collections.deque (' ABCDEFG ') while True:try: print D.pop (), except Index Error: breakprintprint "\nfrom the Left:" D = Collections.deque (xrange (6)) while True:try: print d.popleft (), Except Indexerror: breakprint
With pop () you can delete an element from the right end of the deque and use Popleft () to remove an element from the left side of the deque.
From the right:g F E d c B afrom the left:0 1 2 3) 4 5
Because the double-ended queue is thread-safe, you can leverage the contents of the queue from both ends in different threads.
Import collectionsimport Threadingimport timecandle = Collections.deque (xrange (5)) def Burn (Direction, Nextsource): While True: try: next = Nextsource () except Indexerror: break else: print '%8s:%s '% ( direction, next) Time.sleep (0.1) print '%8s done '% direction Returnleft = Threading. Thread (Target=burn, args= (' left ', candle.popleft)) right = Threading. Thread (Target=burn, args= (' right ', Candle.pop)) Left.start () Right.start () Left.join () Right.join ()
Thread alternately handles both ends, deleting the element, knowing that the deque is empty.
left:0 right:4 right:3 left:1 right:2 left do right done
Rotating
Deque Another function can be rotated in either direction, skipping elements.
Import COLLECTIONSD = Collections.deque (xrange) print ' Normal: ', dd= collections.deque (xrange (+)) D.rotate (2) print ' Right roration: ', DD = Collections.deque (xrange (+)) d.rotate ( -2) print ' left roration: ', D
Results:
Normal:deque ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) right Roration:deque ([8, 9, 0, 1, 2, 3, 4, 5, 6, 7]) left Roration:deque ([2, 3, 4, 5, 6, 7, 8, 9, 0, 1])
Let me give you an example:
#-*-Coding:utf-8-*-"" "This is an interesting example, mainly using the Deque rotate method to implement an infinite loop loading animation" "" Import Sysimport Timefrom Collections Import dequefancy_loading = Deque (' >--------------------') while True:print ' \r%s '% '. Join (fancy_loading), Fancy_ Loading.rotate (1) Sys.stdout.flush () Time.sleep (0.08)
Output Result: