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,依此类推的整数. 列 ...
随机推荐
- R.java不能自动更新
1. The type R is already defined. (很多时候我们在导入其他人的程序的时候,会遇到这个错误) 通常在project里有两个R.java,一个在src,一个在gen,通常 ...
- 【模块应用】MFRC522开发笔记
一.了解基本概念 ①ISO-14443A协议:( 国际标准化组织:International Organization for Standardization)RFID协议的一种; PICC:临近 ...
- PP
- Ubuntu系统下运行Eclipse出现找不到jre的问题的解决方法
在Ubuntu的某些版本下,比如10.10,会出现以下奇怪问题: 1. 安装jdk 我下载的jdk是bin格式的,直接运行解压,得到一个文件夹. 这个文件夹作为jdk的安装目录,可以拷贝到任意目录. ...
- 【LeetCode】Rotate Array
Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = ...
- iOS开发:集成支付宝(遇见的坑和便捷撸代码)
开发iOS最重要的就是支付了,天朝之内最常用的就是支付宝了,下面就以自己的经历说明如何集成支付宝+遇见的坑. 首先,集成支付宝最好别使用Cocoapods,很多人都说使用起来很方便,可是我每次只要使用 ...
- 多线程编程3 - NSOperationQueue
一.简介 一个NSOperation对象可以通过调用start方法来执行任务,默认是同步执行的.也可以将NSOperation添加到一个NSOperationQueue(操作队列)中去执行,而且是异步 ...
- LeetCode Rectangle Area (技巧)
题意: 分别给出两个矩形的左下点的坐标和右上点的坐标,求他们覆盖的矩形面积? 思路: 不需要模拟,直接求重叠的部分的长宽就行了.问题是如果无重叠部分,注意将长/宽给置为0. class Solutio ...
- angularjs 不同的controller之间值的传递
Sharing data between controllers in AngularJS I wrote this article to show how it can possible to pa ...
- 如何重启Activity
有的时候,我们只是想重启某个Activity,但是不重启整个App. 一种做法是: Intent intent = getIntent(); overridePendingTransition(0, ...