《Python核心编程(第2版)》读书笔记(6)之用列表模拟堆栈(关键词:Python/列表/堆栈/stack.py)
来源:互联网 发布:北京sql培训 编辑:程序博客网 时间:2024/05/18 07:45
stack = []def pushit(): stack.append(raw_input('enter new string: ').strip())def popit(): if len(stack)==0: print 'can not pop from an empty stack!' else: # 用反单引号(`)来代替repr()函数,把字符串的内容用引号括起来显示,而不是单单显示字符串的内容。 print 'removed [', `stack.pop()`, ']'def viewstack(): print stack # calls str() internallyCMDs = {'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()[0].lower() except (EOFError,KeyboardInterrupt,IndexError): choice = 'q' print '\nyou picked: [%s]' % choice if choice not in 'uovq': print 'invalid option, try again' else: break if choice == 'q': break CMDs[choice]()if __name__ == '__main__': showmenu()
参考文献:
1.《Python核心编程(第2版)》6.15;
2.用Python实现栈
阅读全文
0 0
- 《Python核心编程(第2版)》读书笔记(6)之用列表模拟堆栈(关键词:Python/列表/堆栈/stack.py)
- 《Python核心编程(第2版)》读书笔记(7)之把列表用作队列(关键词:Python/列表/队列/queue.py)
- Python核心编程:用列表模拟堆栈
- 《Python核心编程(第2版)》读书笔记(2)之赋值(关键词:Python/赋值/引用/传递)
- 《Python核心编程(第2版)》读书笔记(1)之__init__(关键词:Python/面向对象/__init__)
- 《Python核心编程(第2版)》读书笔记(8)之析构(关键词:Python/析构/析构函数)
- 《Python核心编程(第2版)》读薄(5)之dir()(关键词:Python/dir())
- 《Python语言及其应用》读书笔记(2)之第3章Python容器:列表、元组、字典与集合(关键词:列表/元组/字典/集合)
- 《Python核心编程(第2版)》读书笔记(4)之浅拷贝、深拷贝(关键词:Python/浅拷贝/深拷贝)
- 《Python学习手册》学习笔记(8)之第8章列表与字典(关键词:编程语言/Python/列表/字典)
- 《Python核心编程(第2版)》读书笔记(3)之下划线标识符(关键词:Python/变量名/下划线/私有变量)
- Python核心编程---读书笔记:第6章 序列:字符串、列表和元祖
- 《Python核心编程》(第二版) 第6章 序列:字符串、列表和元组(字符串篇)
- python核心编程笔记(5)- 列表,元组
- /STACK(堆栈分配)
- 堆栈(Stack)
- /STACK(堆栈分配)
- 堆栈(stack)
- VT开启教程
- 【记录】Python小伎俩
- img标签总结
- omnet++ 分配信道的问题
- 策略设计模式
- 《Python核心编程(第2版)》读书笔记(6)之用列表模拟堆栈(关键词:Python/列表/堆栈/stack.py)
- CS231n学习笔记--4.Backpropagation and Neural Networks
- Java并发工具包使用指南(全)
- int、int_32t、int_64t
- Aizu 2304 Reverse Roads 费用流
- 数据结构上机实践第二周项目2- 程序的多文件组织
- 以太网帧格式
- 正则表达式 分组
- MySQL 存储引擎(MyISAM、InnoDB、NDBCluster)