• 技术文章 >后端开发 >Python教程

    Python实现队列的方法

    2016-06-06 11:17:53原创613
    本文实例讲述了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程序设计有所帮助。

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:Python 队列
    上一篇:Django中使用group_by的方法 下一篇:python实现用于测试网站访问速率的方法
    千万级数据并发解决方案

    相关文章推荐

    • Python模块化整理和第三方模块的安装(总结分享)• python列表详解(总结分享)• 详细了解Python进程池与进程锁• 一起聊聊Python的编码样式• 带你搞懂Python反序列化
    1/1

    PHP中文网