python 栈和队列(使用list实现)
5.1.1. Using Lists as Stacks
The list methods make it very easy to use a list as a stack, where the last element added is the first element retrieved (“last-in, first-out”). To add an item to the top of the stack, use append(). To retrieve an item from the top of the stack, use pop() without an explicit index. For example:
>>> stack = [3, 4, 5]
>>> stack.append(6)
>>> stack.append(7)
>>> stack
[3, 4, 5, 6, 7]
>>> stack.pop()
7
>>> stack
[3, 4, 5, 6]
>>> stack.pop()
6
>>> stack.pop()
5
>>> stack
[3, 4]
5.1.2. Using Lists as Queues
It is also possible to use a list as a queue, where the first element added is the first element retrieved (“first-in, first-out”); however, lists are not efficient for this purpose. While appends and pops from the end of list are fast, doing inserts or pops from the beginning of a list is slow (because all of the other elements have to be shifted by one).
To implement a queue, use collections.deque which was designed to have fast appends and pops from both ends. For example:
>>> from collections import deque
>>> queue = deque(["Eric", "John", "Michael"])
>>> queue.append("Terry") # Terry arrives
>>> queue.append("Graham") # Graham arrives
>>> queue.popleft() # The first to arrive now leaves
'Eric'
>>> queue.popleft() # The second to arrive now leaves
'John'
>>> queue # Remaining queue in order of arrival
deque(['Michael', 'Terry', 'Graham'])
python 栈和队列(使用list实现)的更多相关文章
- Python 栈、队列的实现
在python中,列表既可以作为栈使用,又可以作为队列使用. 把列表作为栈使用 栈:后进先出 stack=[1,2,3] stack.append(4) #入栈,以列表尾部为栈顶 print(stac ...
- python栈、队列、文件目录遍历
一. 栈与队列 关注公众号"轻松学编程"了解更多. 1. 栈 stack 特点:先进先出[可以抽象成竹筒中的豆子,先进去的后出来] 后来者居上 mystack = [] #压栈[向 ...
- Python 栈和队列,双向队列
# 栈 # 特点: 先进后出 class StackFullException(Exception): pass class StackEmptyException(Exception): pass ...
- python - 栈与队列(只有代码)
1. 栈: - 后进先出 class Stack(object): def __init__(self): self.stack = [] def peek(self): return self.st ...
- python 栈和队列
class Stack: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def push ...
- python数据结构之栈、队列的实现
这个在官网中list支持,有实现. 补充一下栈,队列的特性: 1.栈(stacks)是一种只能通过访问其一端来实现数据存储与检索的线性数据结构,具有后进先出(last in first out,LIF ...
- 【DataStructure In Python】Python模拟栈和队列
用Python模拟栈和队列主要是利用List,当然也可以使用collection的deque.以下内容为栈: #! /usr/bin/env python # DataStructure Stack ...
- python数据结构之栈与队列
python数据结构之栈与队列 用list实现堆栈stack 堆栈:后进先出 如何进?用append 如何出?用pop() >>> >>> stack = [3, ...
- python之单例模式、栈、队列和有序字典
一.单例模式 import time import threading class Singleton(object): lock = threading.RLock() # 定义一把锁 __inst ...
随机推荐
- ionic build Android错误记录未解决
1.try itcordova -v cordova create testing cd testing cordova plugin add cordova-plugin-sim cordova p ...
- c#程序中对密码进行加密的方法
在ADO.NET中,向数据库添加数据时,怎样对数据中的密码进行加密?(也就是说在数据表中也看不到用户的密 码,只是一些经过编译后的字符串,以防止数据库管理员利用用户的密码进行非法操作.) 首先, ...
- R语言内存管理
http://www.cnblogs.com/cloudtj/articles/5478281.html
- Mac无法写入移动硬盘
1.使用mac 磁盘工具,格式化移动硬盘 频繁穿插于mac于pc之间者,大批量拷贝大型文件者,请用exfat 首先,排除Fat32,虽然这是兼容性最好的文件格式,但一个不支持4g以上文件的格式(现在随 ...
- 简述JavaScript对象、数组对象与类数组对象
问题引出 在上图给出的文档中,用JavaScript获取那个a标签,要用什么办法呢?相信第一反应一定是使用document.getElementsByTagName('a')[0]来获取.同样的,在使 ...
- CodeIgniter 3.0问题集锦
1.由于ci 3.0的session采用文件存储,在配置好session存储的目录后,在使用时如果遇到如下session错误. 此处 fc 目录为我设置的session目录. 解决将fc目录权限设为7 ...
- 线程的Alertable与User APC
在使用插User APC注入DLL时,经常面临一个问题,那就是线程必须是处于Alertable模式才能注入成功.但一直对这个Alertable的含义不甚清楚,今天总算是把这个梗消化了. 微软对Aler ...
- js Date学习
Date.parse()接收一个表示日期的字符串参数(参数错误时返回NaN),返回相应日期的毫秒数.(使用自 UTC(Coordinated Universal Time,国际协调时间)1970 年 ...
- WebService的工作原理
Web Service全称XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的独立的通讯技术.是:通过SOAP ...
- pythonchallenge 解谜 Level 8
#-*- coding:utf-8 -*- #代码版本均为python 3.5.1 #Level 7 import bz2 un=b'BZh91AY&SYA\xaf\x82\r\x00\x00 ...