Python實現(xiàn)隊列的方法
更新時間:2015年05月26日 11:57:15 作者:buaa_shang
這篇文章主要介紹了Python實現(xiàn)隊列的方法,實例分析了Python實現(xiàn)隊列的相關(guān)技巧,需要的朋友可以參考下
本文實例講述了Python實現(xiàn)隊列的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
#!/usr/bin/env python
queue = []
def enQ():
queue.append(raw_input('Enter new string: ').strip())
#調(diào)用list的列表的pop()函數(shù).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程序設(shè)計有所幫助。
您可能感興趣的文章:
相關(guān)文章
Python3.4學(xué)習(xí)筆記之 idle 清屏擴展插件用法分析
這篇文章主要介紹了Python3.4 idle 清屏擴展插件用法,簡單分析了idle清屏的幾種方法及idle清屏插件的相關(guān)使用技巧,需要的朋友可以參考下2019-03-03
pycharm最新免費激活碼至2099年(21.3.18親測可用)
這篇文章主要介紹了pycharm最新的激活碼及激活碼的使用方法,幫助大家更好的利用pycharm學(xué)習(xí)python,感興趣的朋友可以了解下。2021-03-03

