Python - 实现Stacks 和Queues
2017-08-25 10:20
351 查看
python没有像C++那样的struct 来实现栈 和队列,但是可以用deque来实现栈和队列,当然,栈也可以用最简单的list来实现。
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:
用deque实现Stacks
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
4000
from both ends. For example:
queue是多线程中的使用的栈,但是Python 解释器有一个全局解释器锁(PIL),导致每个 Python 进程中最多同时运行一个线程,因此 Python 多线程程序并不能改善程序性能,不能发挥多核系统的优势。
multiprocessing.Queue是Python 2.6 引入的用来实现多进程的一种高性能栈。
collections.deque是为了高效实现插入和删除操作的双向列表,适合用于队列和栈。
事实证明:collections.deque比前两种效率都高,可以参考高性能Python之:Queue,deque,queue对比
实现 Stacks
用lists实现StacksThe 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) print(stack) #output : [3, 4, 5, 6, 7] stack.pop() #output : 7 print(stack) #output : [3, 4, 5, 6]
用deque实现Stacks
from collections import deque stack1 = deque([3, 4, 5]) stack1.append(6) stack1.append(7) print(stack1) #output : deque([3, 4, 5, 6, 7]) stack1.pop() #output : 7 print(list(stack1)) #output : [3, 4, 5, 6]
实现 Queues
用deque实现QueuesIt 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
4000
from both ends. For example:
from collections import deque queue = deque([3, 4, 5]) queue.append(6) queue.append(7) print(queue) #output : deque([3, 4, 5, 6, 7]) queue.popleft() #output : 3 print(list(queue)) #output : [4, 5, 6, 7]
queue是多线程中的使用的栈,但是Python 解释器有一个全局解释器锁(PIL),导致每个 Python 进程中最多同时运行一个线程,因此 Python 多线程程序并不能改善程序性能,不能发挥多核系统的优势。
multiprocessing.Queue是Python 2.6 引入的用来实现多进程的一种高性能栈。
collections.deque是为了高效实现插入和删除操作的双向列表,适合用于队列和栈。
事实证明:collections.deque比前两种效率都高,可以参考高性能Python之:Queue,deque,queue对比
相关文章推荐
- Chapter 3 | Stacks and Queues--两个栈实现队列
- Chapter 3 | Stacks and Queues--实现数据结构模拟一个栈由几个子栈组成,并可以在指定子栈上进行出栈操作
- Chapter 3 | Stacks and Queues--一个数组实现三个栈(续)
- Chapter 3 | Stacks and Queues--一个数组实现三个栈
- Chapter 3 | Stacks and Queues--实现一个函数返回栈中的最小值,时间复杂度为O(1)
- Python 实现简单的命名合法性检查
- Python+Selenium 自动化实现实例-获取页面元素信息
- 普林斯顿算法课Part 1 Week 2 Stacks and Queues
- Python通过select实现异步IO的方法
- python实现连接mongodb的方法
- Python实现各类数据结构和算法---动态规划之最长公共子序列
- python实现html转ubb代码(html2ubb)
- PCA(主成份分析法)技术及其Python实现
- python实现本地图片转存并重命名的示例代码
- python+soket实现UDP协议的局域网广播程序
- [置顶] 关于求N以内素数的python实现以及优化方法
- LeetCode 93 Restore IP Addresses(Python详解及实现)
- 最小二乘法 python实现
- 剑指offer之Python实现(一)