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实现)的更多相关文章

  1. Python 栈、队列的实现

    在python中,列表既可以作为栈使用,又可以作为队列使用. 把列表作为栈使用 栈:后进先出 stack=[1,2,3] stack.append(4) #入栈,以列表尾部为栈顶 print(stac ...

  2. python栈、队列、文件目录遍历

    一. 栈与队列 关注公众号"轻松学编程"了解更多. 1. 栈 stack 特点:先进先出[可以抽象成竹筒中的豆子,先进去的后出来] 后来者居上 mystack = [] #压栈[向 ...

  3. Python 栈和队列,双向队列

    # 栈 # 特点: 先进后出 class StackFullException(Exception): pass class StackEmptyException(Exception): pass ...

  4. python - 栈与队列(只有代码)

    1. 栈: - 后进先出 class Stack(object): def __init__(self): self.stack = [] def peek(self): return self.st ...

  5. python 栈和队列

    class Stack: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def push ...

  6. python数据结构之栈、队列的实现

    这个在官网中list支持,有实现. 补充一下栈,队列的特性: 1.栈(stacks)是一种只能通过访问其一端来实现数据存储与检索的线性数据结构,具有后进先出(last in first out,LIF ...

  7. 【DataStructure In Python】Python模拟栈和队列

    用Python模拟栈和队列主要是利用List,当然也可以使用collection的deque.以下内容为栈: #! /usr/bin/env python # DataStructure Stack ...

  8. python数据结构之栈与队列

    python数据结构之栈与队列 用list实现堆栈stack 堆栈:后进先出 如何进?用append 如何出?用pop() >>> >>> stack = [3, ...

  9. python之单例模式、栈、队列和有序字典

    一.单例模式 import time import threading class Singleton(object): lock = threading.RLock() # 定义一把锁 __inst ...

随机推荐

  1. 数据结构0103汉诺塔&八皇后

    主要是从汉诺塔及八皇后问题体会递归算法. 汉诺塔: #include <stdio.h> void move(int n, char x,char y, char z){ if(1==n) ...

  2. 动态生成一个设定好特殊样式的Tlabel,快速生成代码

    动态生成一个设定好特殊样式的Tlabel,快速生成代码: 1.自己先在可视化界面设定一个Label,像这样: 2.选择label,快捷键ctrl+C 复制,粘贴带代码编辑器去,会生成一段这样的窗体代码 ...

  3. Pascal 语言中的关键字及保留字

    absolute //指令(变量) abstract //指令(方法) and //运算符(布尔) array //类型 as //运算符(RTTI) asm //语句 assembler //向后兼 ...

  4. iOS 使点击事件穿透透明的UIView

    如图: 悬浮的三个按钮下方有一个可以点击的灰色区域,但是点击按钮之间的透明区域, 这三个按钮的contentView会响应这个点击事件,这时候需要让这个contentView不响应这个点击事件. 解决 ...

  5. CentOS6.5的vsftp搭建流程(一)

    前几次搭建FTP都失败了,不是登陆不了,就是目录没有权限.现在终于摸索出了靠谱的操作流程,分享之~ 1. 查看是否安装了vsftpd,未安装则安装 [root@iZ283tian2dZ /]# rpm ...

  6. HDU5509 : Pattern String

    只要求出两个字符串的最小表示,然后就可以判断是否循环同构. 枚举最小表示的开头在哪个位置,然后求出Hash值,如果两个串的Hash值集合有交,那么说明循环同构. 因为串经过压缩,原串的长度很大,不能直 ...

  7. Logical query-processing phases

    Logical query-processing phases in brief (1) FROM This phase identifies the query’s source tables an ...

  8. Codeforces Round #257 (Div. 2)

    A - Jzzhu and Children 找到最大的ceil(ai/m)即可 #include <iostream> #include <cmath> using name ...

  9. ACM: Gym 101047K Training with Phuket's larvae - 思维题

     Gym 101047K Training with Phuket's larvae Time Limit:2000MS     Memory Limit:65536KB     64bit IO F ...

  10. 栈的JS实现

    栈,是一种特殊的线性表,其插入及删除的操作都在线性表的同一端进行.这一端称为栈顶,另一端称为栈底.就类似于餐厅里的一摞盘子,后放的盘子在上方,也会先被人拿走.栈具有"后进先出"的逻 ...