Python实现队列的方法

yipeiwu_com6年前Python基础

本文实例讲述了Python实现队列的方法。分享给大家供大家参考。具体实现方法如下:

#!/usr/bin/env python 
queue = [] 
def enQ(): 
  queue.append(raw_input('Enter new string: ').strip())
#调用list的列表的pop()函数.pop(0)为列表的第一个元素 
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: 
    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 'Invalid option, try again' 
      else: 
        break 
    if choice == 'q': 
      break 
    CMDs[choice]() 
if __name__ == '__main__': 
  showmenu()

希望本文所述对大家的Python程序设计有所帮助。

相关文章

详解python 模拟豆瓣登录(豆瓣6.0)

详解python 模拟豆瓣登录(豆瓣6.0)

最近在学习python爬虫,看到网上有很多关于模拟豆瓣登录的例子,随意找了一个试了下,发现不能运行,对比了一下代码和豆瓣网站,发现原来是豆瓣网站做了修改,增加了反爬措施。 首先看下要模拟...

python排序方法实例分析

本文实例讲述了python排序方法。分享给大家供大家参考。具体如下: >>> def my_key1(x): ... return x % 10 ... >...

深入浅析Python中的迭代器

深入浅析Python中的迭代器

目录结构: contents structure [-] 在开始文章之前,先贴上一张Iterable、Iterator与Generator之间的关系图:   1. Itera...

Python中正则表达式的用法总结

正则表达式很神奇啊 # -*- coding:utf-8 -*- import re def print_match_res(res): """打印匹配对象内容""" if...

详解Python nose单元测试框架的安装与使用

详解Python nose单元测试框架的安装与使用

本文介绍了Python nose单元测试框架的安装与使用 ,分享给大家,具体如下: 安装(Python2下安装) pip install nose 原理与命名规则 Nose会自动查...