From collectionsImport deque
#Bidirectional queue[Ordered sequence] (PackagingList
DefDeques ():
#Bidirectional queue,Thread Safety,Both sides of the queue are added and ejected with a complexity ofO (1),Very high efficiency
#Create
Lists = [A,B,C,D,"E"]# <class ' list ';: [' A ', ' B ', ' C ', ' D ', ' E ']
Queue = deque (lists)# deque ([' A ', ' B ', ' C ', ' D ', ' E '])
Queue = deque (lists,3)# 3To limit container size,After the queue is full,The element that is added first will extrude the old element from the other end;
#No container size specified will be any length, deque ([' C ', ' D ', ' E '], maxlen=3)
Queue = Deque (Range10))# deque ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
#Add to
Queue.append ("F")#Add to right, Deque ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ' F '])
Queue.appendleft ("G")#Add to Left, deque ([' G ', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ' F '])
Queue.extend (Range3))#Add to rightIterableElements, deque ([' G ', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ' F ', 0, 1, 2])
Queue.extendleft (Range3))#Append to the left, notice becomes210,Deque ([2, 1, 0, ' G ', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ' F ', 0, 1, 2])
Queue.insert (1,"L")#In the specifiedIndexInsert Element,Deque ([2, ' L ', 1, 0, ' G ', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ' F ', 0, 1, 2])
Queue = queue + deque (Range3))#MergeDeque ([2, ' L ', 1, 0, ' G ', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ' F ', 0, 1, 2, 0, 1, 2])
Queue = Queue *3# NTimes
#Get
Queue = deque (lists)# deque ([' A ', ' B ', ' C ', ' D ', ' E '])
Queue2 = Queue.copy ()#Shallow copy
index = Queue.index ("C")#Get index,Not found thrownValueErrorAbnormal, 2
index = Queue.index (C,1)#can specifyStart
index = Queue.index (C,2,4)#can specifyEnd
Elem =Max (queue)# E
Elem =Min (queue)#
#Statistics
Count = Queue.count ("A")# 1
#Delete
Elem = Queue.pop ()#Eject right element, deque ([' A ', ' B ', ' C ', ' D '])
Elem = Queue.popleft ()#Eject left Element, deque ([' B ', ' C ', ' D '])
Queue.remove ("C")# deque ([' B ', ' D '])
Queue.clear ()#Empty
# pointer
queue = deque (lists) # deque ([' A ', ' B ', ' C ', ' D ', ' E '])
Queue.rotate (-1) # rotate pointer + element move - element move forward
# other
Queue.reverse () # invert element , deque ([' A ', ' E ', ' D ', ' C ', ' B ']) /span>
python3-note-b-006-data structure-bidirectional queue deque