The example in this article describes how Python implements the queue. Share to everyone for your reference. The implementation method is as follows:
#!/usr/bin/env python queue = [] def enQ (): queue.append (Raw_input (' Enter new string: '). Strip ()) #调用list的列表的pop () function. Pop (0) for the first element of the list Def DeQ (): If len (queue) = = 0: print ' cannot pop from an empty queue! ' else: print ' Removed [', Queue.pop (0), '] ' Def viewq (): print Queue CMDs = {' E ': EnQ, ' d ': DeQ, ' V ': viewq} def SH Owmenu (): PR = "" " (E) nqueue (D) equeue (V) iew (Q) uit Enter choice:" "While True: While True: try: choice = raw_input (pr). Strip () [0].lower () except (Eoferror, Keyboardinterrupt , Indexerror): choice = ' Q ' print ' \nyou picked: [%s] '% choice if choice not in ' Devq ': print ' Invali d option, try again ' else: break if choice = = ' Q ': Break Cmds[choice] () if __name__ = = ' __main_ _ ': ShowMenu ()
Hopefully this article will help you with Python programming.