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)文章
Python?pyecharts?Map地圖數(shù)據(jù)不顯示的原因及完美解決
這篇文章主要給大家介紹了關(guān)于Python?pyecharts?Map地圖數(shù)據(jù)不顯示的原因及解決辦法,pyecharts是一款將python與echarts結(jié)合的強大的數(shù)據(jù)可視化工具,文中通過圖文以及代碼示例介紹的非常詳細,需要的朋友可以參考下2023-12-12
CentOS下使用yum安裝python-pip失敗的完美解決方法
這篇文章主要介紹了CentOS下使用yum安裝python-pip失敗的完美解決方法,需要的朋友可以參考下2017-08-08
用python簡單實現(xiàn)mysql數(shù)據(jù)同步到ElasticSearch的教程
今天小編就為大家分享一篇用python簡單實現(xiàn)mysql數(shù)據(jù)同步到ElasticSearch的教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05
python基礎(chǔ)while循環(huán)及if判斷的實例講解
下面小編就為大家?guī)硪黄猵ython基礎(chǔ)while循環(huán)及if判斷的實例講解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08

