Python analog recording stack operation queue (to be continued)

Stack: data is last in first out (LIFO) last in first out

Queue: data FIFO (FIFO) first in first out

 

The first is a list :( either analog stack can be simulated queue) a good to poor.

A container is a sequence listing, but also in the simulated stack operations, the speed comparison is possible.

When simulation stack operations, must append to pop. (Stack simulation time, excellent performance)

The In [228]: LL = List ()                                                                    

the In [229]: ll.append ( 'Task. 1')                                                             

the In [230]: ll.append ( 'task 2')                                                             

the In [231]: ll.append ( 'task 3 ')                                                             

the In [232]: ll.pop (0)                                                                      
Out [232]:' tasks. 1 ' 

the In [233]: ll.pop (0)                                                                      
Out [233]:' task 2 ' 

the In [234]: ll.pop (0)                                                                      
Out [234]: 'task 3'

 Such simulation operation queue is very inefficient because every time you are out of the first element, the latter element index will change

Professional time required argument is O (n)

 

The second is collections.deque, that a simulated stack are excellent in the queue container sequence.

In addition to poor performance of a random access intermediate element of the object, time-consuming to O (n), but half of the sequence of operation of the vessel, take few accesses to the intermediate element.

Analog stack operations:

In [235]: from collections import deque                                                 

In [236]: q = deque?                                                                    
Init signature: deque(self, /, *args, **kwargs)
Docstring:     
deque([iterable[, maxlen]]) --> deque object

A list-like sequence optimized for data accesses near its endpoints.
File:           /usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/collections/__init__.py
Type:           type
Subclasses:     

In [237]: q = deque()                                                                   

In [238]: q.append('任务1')                                                             

In [239]: q.append ( 'task 2')                                                              

the In [240]: q.append ( 'task. 3')                                                              

the In [241]: q.pop ()                                                                        
Out [241]: 'tasks. 3' 

the In [242 ]: q.pop ()                                                                        
Out [242]: 'task 2' 

the In [243]: q.pop ()                                                                        
Out [243]: 'tasks. 1' 

the In [244]: Q                                                                              
Out [244]: the deque ([] )

 Analog queue-operation:

In [246]: q.append ( 'Task. 1')                                                              

the In [247]: q.append ( 'task 2')                                                              

the In [248]: q.append ( 'task. 3')                                                              

the In [249]: Q                                                                              
Out [ 249]: deque ([ 'task 1', 'task 2', 'task. 3']) 

the In [250]: q.popleft ()                                                                    
Out [250]: 'task 1' 

the In [251]: q.popleft ( )                                                                    
Out [251]: 'task 2' 

the In [252]: Q.                                                                   popleft () 
Out [252]: 'task 3'

 

That is the two above can simulate the stack, and can simulate container sequence queue, described below is only a single analog stack or queue.

queue.LifoQueue can be seen from the simulation is literally out of the stack, LIFO. container sequence in the queue module, provides a lock statement to support multiple concurrent producers to consumers. Many examples of the class of the following modules typically used in communication between threads.

Guess you like

Origin www.cnblogs.com/sidianok/p/12071123.html