python3-Notes-b-006-Data structure-bidirectional queue deque

Source: Internet
Author: User
Tags shallow copy

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

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.