Python列表操作——模拟实现栈和队列
1.实现栈:
stack=[] def pushit():
stack.append(raw_input('Enter New String:').strip())
def popit():
if len(stack)==0:
print 'can not pop anything from a empty stack'
else:
print 'Remove[',repr(stack.pop()),']'
def viewstack():
print stack CMDs={'u':pushit,'o':popit,'v':viewstack} def showmenu():
pr="""
p(U)sh
p(O)p
(V)iew
(Q)uit Enter choice:"""
while True:
while True:
try:
choice=raw_input(pr).strip().lower()
except (EOFError,KeyboardInterrupt,IndexError):
choice='q'
print '\nYou picked:[%s]' %choice
if choice not in 'uovq':
print'Invaild option,try again'
else:
break
if choice=='q':
break
CMDs[choice]() if __name__=='__main__':
showmenu()
2.实现队列
queue=[] def enQ():
queue.append(raw_input('Please enter a new element:').strip()) def deQ():
if len(queue)==0:
print "Error: cannot pop anything from a empty queue"
else:
print 'Remove [',queue.pop(0),']' def viewshow():
print queue
CMDs={'e':enQ,'d':deQ,'v':viewshow}
def showmenu():
pr='''
(E)nQ
(D)eQ
(V)iewshow
(Q)uit Enter your choice:
'''
while True:
while True:
try:
choice=raw_input(pr).strip()[0].lower()
except (EOFError,KeyboardInterrupt,IndexError):
choice='q'
print 'You picked [%s]' % choice
if choice not in 'edvq':
print 'Invail option, Try again'
else:
break
if choice=='q':
break
CMDs[choice]()
if __name__=='__main__':
showmenu()
Python列表操作——模拟实现栈和队列的更多相关文章
- Python列表操作大全(非常全)
Python列表操作大全(非常全!!!) 对于python列表的理解可以和C语言里面的数组进行比较性的记忆与对照,它们比较相似,对于python里面列表的定义可以直接用方括号里加所包含对象的方法,并且 ...
- python第七篇:Python 列表操作详解
Python列表操作详解 list函数 list() #生成一个空的列表 list(iterable) #用可迭代对象初始化一个列表 列表的 and 运算和 or 运算 列表and运算 > ...
- python列表操作大全
Python列表操作大全 对于python列表的理解可以和C语言里面的数组进行比较性的记忆与对照,它们比较相似,对于python里面列表的定义可以直接用方括号里加所包含对象的方法,并且python的列 ...
- Python列表操作集合
对于python列表里元素的操作主要分为以下几个方面: 1.向列表里面加元素: 向python列表里面添加元素主要有三种方法: (1)append() append()对于列表的操作主要实现的是在特定 ...
- Python列表操作与深浅拷贝(5)——数字处理函数、类型判断、列表链表队列栈
python内建数据结构 分类 数值型: int float complex bool 序列对象: 字符串str 列表list 元组tuple 键值对: 集合set 字典dict 数值型 (list ...
- python列表操作总结
list是python中非常重要的类型/数据结构,总结如下: 符号说明 l:列表 l2:新列表 e:元素 index:位置 方法: 原地修改: l.append(e) l.insert(index, ...
- python threading模块使用 以及python多线程操作的实践(使用Queue队列模块)
今天花了近乎一天的时间研究python关于多线程的问题,查看了大量源码 自己也实践了一个生产消费者模型,所以把一天的收获总结一下. 由于GIL(Global Interpreter Lock)锁的关系 ...
- Python :用两个栈实现队列
转自:http://blog.csdn.net/Lynette_bb/article/details/75092745 牛客网上的剑指 offer的在线编程: 题目描述 用两个栈来实现一个队列,完成队 ...
- Python列表操作常用API
1.列表的概念 (1)列表的定义 列表是Python中一种基本的数据结构.列表存储的数据,我们称为元素.在列表中的每个元素都会有一个下标来与之对应,第一个索引是0,第二个索引是1,依此类推的整数. 列 ...
随机推荐
- Android Context
http://www.cnblogs.com/android100/p/Android-Context.html
- Python网络编程03----Python3.*中socketserver
socketserver(在Python2.*中的是SocketServer模块)是标准库中一个高级别的模块.用于简化网络客户与服务器的实现(在前面使用socket的过程中,我们先设置了socket的 ...
- IOS图片缩放
1.自动缩放到指定大小 + (UIImage *)thumbnailWithImage:(UIImage *)image size:(CGSize)asize { UIImage *newimage; ...
- LeetCode----Unique Binary Search Trees 2
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- 通过 itms-services 协议,发布或者分享 iOS 应用程序
导读:itms-services 协议常用于 iOS 企业应用的无线部署,这可在不使用 iTunes 的情况下将内部软件发布或者分享给用户. 一.前期准备资料: 1.应用程序 (.ipa) 文件(使用 ...
- iOS打包为ipa的两种方式和生成P12证书的方式
iOS项目打包为ipa的两种方式: 准备工作:先行在Xcode里面打开preferences,填写apple id. 通过iTunes+Xcode 在Xcode里,把模拟器调整为iOS Device, ...
- Android RecyclerView添加Header头部
Android RecyclerView添加Header头部 Android RecyclerView不像以前的ListView那样直接添加头部,如果要给RecyclerView增加头部,则需要 ...
- C#指针操作Marshal实例
static void Main(string[] args) { ,,,}; ,,,}; IntPtr pt = Marshal.AllocHGlobal(a.Length); //从source数 ...
- PHP 防范xss攻击
XSS 全称为 Cross Site Scripting,用户在表单中有意或无意输入一些恶意字符,从而破坏页面的表现! 看看常见的恶意字符XSS 输入: 1.XSS 输入通常包含 JavaScript ...
- Oracle sysdate
SYSDATE --可得到目前系统的时间 ex. select sysdate from dual; sysdate ---------- 20-SEP-07 ...