Python implements the queue method, and python implements the queue

Source: Internet
Author: User

Python implements the queue method, and python implements the queue

This example describes how to implement a 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.

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.