This article mainly introduces how to implement the queue in Python. The example analyzes the skills related to implementing the queue in Python. if you need it, refer to the example in this article to illustrate how to implement the queue in Python. Share it with you for your reference. The specific implementation method is as follows:
#! /Usr/bin/env python queue = [] def enQ (): queue. append (raw_input ('enter new string :'). strip () # Call the pop () function of the list. pop (0) is the first element in 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 showmenu (): pr = "(E) nqueue (D) equeue (V) iew (Q) uit Enter choice:" while True: try: choice = raw_input (pr ). strip () [0]. lower () handle T (EOFError, KeyboardInterrupt, IndexError): choice = 'Q' print '\ nYou picked: [% s]' % choice if choice not in 'devq ': print 'invalid option, try again 'else: break if choice = 'Q': break CMDs [choice] () if _ name _ = '_ main _': showmenu ()
I hope this article will help you with Python programming.