Python实现队列的方法

yipeiwu_com5年前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程序设计有所帮助。

相关文章

pandas把所有大于0的数设置为1的方法

如下所示: df = pd.read_csv(‘hahaha.csv') df[df>0] = 1 print(df) 以上这篇pandas把所有大于0的数设置为1的方法...

Python中os.path用法分析

本文实例分析了Python中os.path用法。分享给大家供大家参考。具体如下: 复制代码 代码如下:#coding=utf-8 import os print os.path.absp...

python开发中range()函数用法实例分析

本文实例讲述了python开发中range()函数用法。分享给大家供大家参考,具体如下: python中的range()函数的功能很强大,所以我觉得很有必要和大家分享一下 就好像其API...

python实现猜单词小游戏

Python初学者小游戏:猜单词,供大家参考,具体内容如下 游戏逻辑:就像我们曾经英语学习机上的小游戏一样,电脑会从事先预置的词库中抽取单词,然后给出单词的字母数量,给定猜解次数,然后让...

python生成验证码图片代码分享

python生成验证码图片代码分享

本文实例为大家分享了python生成验证码图片代码,分享给大家供大家参考,具体内容如下 基本上大家使用每一种网络服务都会遇到验证码,一般是网站为了防止恶意注册、发帖而设置的验证手段。其生...