python collections deque
collections是python的高级容器类库,包含了dict、truple之外的常用容器。
下面介绍常用的deque
1. deque是双端队列,可以从两端塞元素进去,也可以从两端取元素。
2. deque是线程安全的,可以用来做多线程的共享资源,我也是因为这个开始接触duque的
>>> from collections import deque
>>> a = [1, 2, 3, 4]
用列表初始化deque
>>> deq = deque(a)
>>> deq
deque([1, 2, 3, 4])
往deque中迭代入队列表原始
>>> deq.extend(a)
>>> deq
deque([1, 2, 3, 4, 1, 2, 3, 4])
从右端入队元素
>>> deq.append(5)
>>> deq
deque([1, 2, 3, 4, 1, 2, 3, 4, 5])
从左端输入元素
>>> deq.appendleft(6)
>>> deq
deque([6, 1, 2, 3, 4, 1, 2, 3, 4, 5])
从右端出队
>>> deq.pop()
5
从左端出队
>>> deq.popleft()
6
>>> deq
deque([1, 2, 3, 4, 1, 2, 3, 4])
从左往右旋转
>>> deq.rotate(-2)
>>> deq
deque([3, 4, 1, 2, 3, 4, 1, 2])
从右往左旋转
>>> deq.rotate(2)
>>> deq
deque([1, 2, 3, 4, 1, 2, 3, 4])
从文件输出到deque
比如,输入文件的倒数3行,得到大小为3的队列
>>> a = deque(open('test.txt'), 3)
>>> a
deque(['5\n', '6\n', '7\n'], maxlen=3)
python collections deque的更多相关文章
- 739. Daily Temperatures && 单调栈 && Python collections deque
题目大意 给你接下来每一天的气温,求出对于每一天的气温,下一次出现比它高气温的日期距现在要等多少天 解题思路 利用单调栈,维护一个单调递减的栈 将每一天的下标i入栈,维护一个温度递减的下标 若下一个温 ...
- python之保留有限的历史记录(collections.deque)
1.deque(maxlen=N)创建一个固定长度的队列,当有新的记录加入而队列已经满时,会自动移除老的记录. from collections import deque q = deque(maxl ...
- python collections 模块 之 deque
class collections.deque(iterable[,maxlen]): 返回 由可迭代对象初始化的 从左向右的 deque 对象. maxlen: deque 的最大长度,一旦长度超出 ...
- [python] Queue.Queue vs. collections.deque
https://stackoverflow.com/questions/717148/queue-queue-vs-collections-deque/717199#717199 Queue,Queu ...
- Python collections模块总结
Python collections模块总结 除了我们使用的那些基础的数据结构,还有包括其它的一些模块提供的数据结构,有时甚至比基础的数据结构还要好用. collections ChainMap 这是 ...
- (转)python collections模块详解
python collections模块详解 原文:http://www.cnblogs.com/dahu-daqing/p/7040490.html 1.模块简介 collections包含了一些特 ...
- Python Collections详解
Python Collections详解 collections模块在内置数据结构(list.tuple.dict.set)的基础上,提供了几个额外的数据结构:ChainMap.Counter.deq ...
- python的deque(双向)队列详解
首先 python的队列有很多种 Python标准库中包含了四种队列,分别是queue.Queue / asyncio.Queue / multiprocessing.Queue / collecti ...
- python collections defaultdict
class_counts = defaultdict(int) 一.关于defaultdict 在Python里面有一个模块collections,解释是数据类型容器模块.这里面有一个collect ...
随机推荐
- 剑指Offer——和为S的两个数字
题目描述: 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的. 输入描述: 对应每个测试案例,输出两个数,小的先输出. ...
- 为golang程序使用pprof远程查看httpserver运行堆栈,cpu耗时等信息
pprof是个神马玩意儿? pprof - manual page for pprof (part of gperftools) 是gperftools工具的一部分 gperftools又是啥? Th ...
- Using virtual lists
Download demo project - 15.7 Kb Contents Introduction Virtual list Creating a virtual list Add items ...
- jQuery中this与$(this)的差别
jQuery中this与$(this)的差别 $("#textbox").hover( function() { this.title ...
- DevExpress控件学习总结
1.Navigation & Layout 1.1 Bar Manager 如果想在窗体或用户控件(user control)上添加工具条(bars)或弹出菜单(popup menus),我们 ...
- SQL Server 2008数据库手动提交的设置
有时候我们需要对SQL Server 2008数据库手动提交的方法进行设置,使用Oracle的朋友会注意到Oracle中的手工提交的,如果修改错了数据还可以Rollback.但在SQL Server ...
- Singleton: this & instance
public class Singleton{ private static final Singleton instance = new Singleton(); private String na ...
- SQL Server扩展属性的增、删、改、查
使用 sql 语句创建表时,可以使用系统存储过程 sp_addextendedproperty 给字段添加描述说明. sp_addextendedproperty 语法: sp_addextended ...
- [Windows Powershell]-学习笔记(2)
数学运算 我们可以把powershell当成一个计算器.如键入命令行那样输入数学表达式,回车,powershell会自动计算并把结果输出.常用的加减乘除模(+,-,*,/,%)运算和小括号表达式都支持 ...
- ruby中的可调用对象--方法
上一篇讲了ruby中的可调用对象proc和lambda,他们都是块转换成的对象.ruby中的可调用对象还有方法.通过使用method方法,并且以方法名作为参数(字符串或者符号),就可以得到一个方法对象 ...