Deque is an abbreviation for double-ended queue, similar to list, but provides an action to insert and delete at both ends.
- Appendleft inserts to the left of the list
- Popleft the value on the left side of the pop-up list
- Extendleft extension on the left
For example:
Queue = Deque ()
# append values to wait for processing
queue.appendleft ("a")
Queue.appendleft ("second"
Queue.appendleft ("third")
# Pop values when ready
process (Queue.pop ()) # would process "a"
# add Values while processing
queue.appendleft ("fourth")
# What does the queue look like now?
Queue # deque ([' Fourth ', ' third ', ' second '])
As a two-terminal queue, Deque also provides some other useful methods, such as rotate, and so on, let's take a look at the following:
Fill
Deque can be populated from either end and the Python implementation is called the "left" and "right" side.
Import collections
D1 = Collections.deque ()
d1.extend (' ABCDEFG ')
print ' Extend: ', D1
d1.append (' h ')
print ' append: ', d1
d2 = Collections.deque ()
D2.extendleft (xrange (6))
print ' Extendleft ', D2
D2.appendleft (6)
print ' Appendleft ', D2
Extendleft () iterates through its input, completing the same processing for each element as Appendleft ().
Extend:deque ([' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' G ']
append:deque ([' A ', ' B ', ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h '])
Exten Dleft 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 applied algorithm.
Import Collections
print "From the right:"
d = collections.deque (' ABCDEFG ') while
True:
try:
Print D.pop (),
except Indexerror:
break
print
print "\nfrom the Left:"
d = collections.deque ( Xrange (6))
while True:
try:
print d.popleft (),
except Indexerror:
break
Print
Using Pop () you can remove an element from the right side of the deque and use Popleft () to remove an element from the left side of the deque.
From ' right:
G F E d C b A ' left
:
0 1 2 3 4 5
Because a two-terminal queue is thread-safe, the contents of the queue can be leveraged from both ends in different threads.
Import Collections
Import threading
import time
candle = 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
return
left = 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, deletes the element, knows this deque is empty.
left:0 right:4
right:3 left:1
Right:2 left do right do
Rotating
deque Another function can rotate in either direction and skip over some elements.
Import collections
D = Collections.deque (xrange)
print ' Normal: ', D
d= Collections.deque (xrange (10))
d.rotate (2)
print ' Right roration: ', d
d = 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
]) left
Roratio N:deque ([2, 3, 4, 5, 6, 7, 8, 9, 0, 1])
Another example:
#-*-Coding:utf-8-*-"" "The
following is an interesting example, the main use of the deque rotate method to achieve an infinite loop
load animation"
""
Import SYS
import time from
collections Import deque
fancy_loading = deque (' >--------------------')
While True:
print ' \r%s '% '. Join (fancy_loading),
fancy_loading.rotate (1)
Sys.stdout.flush ()
Time.sleep (0.08)
Output results:
> an endless cycle of racing lights------------->-------