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

相关文章

Django 重写用户模型的实现

Django内建的User模型可能不适合某些类型的项目。例如,在某些网站上使用邮件地址而不是用户名作为身份的标识可能更合理。 1.修改配置文件,覆盖默认的User模型 Django允...

python字符串的拼接方法总结

python字符串的拼接方法总结

这篇文章主要介绍了python字符串的拼接方法总结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 加号连接 1.通过+号连接起来 逗...

Django 中使用流响应处理视频的方法

Django 中使用流响应处理视频的方法

起步 利用 html5 的 <video> 标签可以播放: <video width="320" height="240" controls> <so...

python求crc32值的方法

本文实例讲述了python求crc32值的方法。分享给大家供大家参考。具体实现方法如下: 要想求CRC值,前面要import binascii binascii.crc32(v)&nbs...

centos 下面安装python2.7 +pip +mysqld

python2.7 复制代码 代码如下: wget http://www.python.org/ftp/python/2.7.6/Python-2.7.6.tar.xz yum inst...