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

相关文章

Python之列表的插入&替换修改方法

用例子说明 fruit = ['pineapple','grape','pear'] fruit[0:0] = ['Orange'] #在fruit集合中第一位插入字符串'Ora...

numpy向空的二维数组中添加元素的方法

直接上代码了 x = np.empty(shape=[0, 4], int) x = np.append(x, [[1,2,3,4]], axis = 0) x = np.appen...

Windows系统Python直接调用C++ DLL的方法

环境:Window 10,VS 2019, Python 2.7.12, 64bit 1,打开 VS 2019,新建C++ Windows 动态链接库工程 Example,加入下列文件,...

python+selenium实现简历自动刷新的示例代码

python+selenium实现简历自动刷新的示例代码

本文用到的文件的下载地址 百度网盘链接: https://pan.baidu.com/s/1tmpdEfAZKff5TOMAitUXqQ 提取码: e6at 1 安装Python 和 s...

Python实现简单遗传算法(SGA)

Python实现简单遗传算法(SGA)

本文用Python3完整实现了简单遗传算法(SGA) Simple Genetic Alogrithm是模拟生物进化过程而提出的一种优化算法。SGA采用随机导向搜索全局最优解或者说近似...