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

相关文章

django迁移数据库错误问题解决

django.db.migrations.graph.NodeNotFoundError: Migration order.0002_auto_20181209_0031 depen...

基于Python3 逗号代码 和 字符图网格(详谈)

逗号代码 假定有下面这样的列表: spam=['apples','bananas','tofu',' cats'] 编写一个函数,它以一个列表值作为参数,返回一个字符串。该字符串包...

对pandas中Series的map函数详解

Series的map方法可以接受一个函数或含有映射关系的字典型对象。 使用map是一种实现元素级转换以及其他数据清理工作的便捷方式。 (DataFrame中对应的是applymap()函...

Python反转序列的方法实例分析

本文实例讲述了Python反转序列的方法。分享给大家供大家参考,具体如下: 序列是python中最基本的数据结构,序列中每个元素都有一个跟位置相关的序号,也称为索引。对于一个有N个元素的...

详解Python中打乱列表顺序random.shuffle()的使用方法

之前自己一直使用random中 randint生成随机数以及使用for将列表中的数据遍历一次。 现在有个需求需要将列表的次序打乱,或者也可以这样理解: 【需求】将一个容器中的数据每次...