class collections.deque(iterable[,maxlen]):

返回 由可迭代对象初始化的 从左向右的 deque 对象。

maxlen: deque 的最大长度,一旦长度超出,会在 相反方向 删除等量的 items。

append(x): 从 deque 的右边添加

appendleft(x): 从 deque 的左边添加

clear(): 移除 deque 中的所有元素

copy(): 浅拷贝 deque

count(x): 计算 deque 中 x 的数量

extend(x): 从右边扩展 deque

extendleft(x): 从左边扩展 deque

index(x[,start[,stop]]): 返回 出现在deque 中的第一个 x 的位置,可设置索引的起始和结束

insert(x, i): 在 i 位置 插入 x

pop(): 从deque的右边删除

popleft():从deque的左边删除

remove(value): 移除 deque 中的 value

reverse(): 翻转deque

rotate(n=1): 翻转 deque n 步,右边至左边,如果n为负数,则,左边至右边

deque 的应用:

roundrobin:
def roundrobin(*iterables):
# "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
iterators = deque(map(iter, iterables)) # 生成迭代器
while iterators:
try:
while True:
yield next(iterators[0]) # 取出最左边的迭代器的第一个元素
iterators.rotate(-1) # 将迭代器置于最右
except StopIteration:
# Remove an exhausted iterator.
iterators.popleft() # 如果迭代器为空,则删除该迭代器
保存有限的历史记录:
from collections import deque

def search(lines, pattern, history=5):
previous_lines = deque(maxlen=history)
for line in lines:
if pattern in lines:
yield line, previous_lines
previous_lines.append(line) if __name__ == '__main__':
with open('somefile.txt') as f:
for line, prevlines in search(f, 'python', 5):
for pline in prevlines:
prtin(pline, end='')
print(line, end='')
print('-'*20)
 

python collections 模块 之 deque的更多相关文章

  1. Python collections模块总结

    Python collections模块总结 除了我们使用的那些基础的数据结构,还有包括其它的一些模块提供的数据结构,有时甚至比基础的数据结构还要好用. collections ChainMap 这是 ...

  2. (转)python collections模块详解

    python collections模块详解 原文:http://www.cnblogs.com/dahu-daqing/p/7040490.html 1.模块简介 collections包含了一些特 ...

  3. python collections模块

    collections模块基本介绍 collections在通用的容器dict,list,set和tuple之上提供了几个可选的数据类型 namedtuple() factory function f ...

  4. Python collections 模块用法举例

    Python作为一个“内置电池”的编程语言,标准库里面拥有非常多好用的模块.比如今天想给大家 介绍的 collections 就是一个非常好的例子. 1.collections模块基本介绍 我们都知道 ...

  5. Python——collections模块、time模块、random模块、os模块、sys模块

    1. collections模块 (1)namedtuple # (1)点的坐标 from collections import namedtuple Point = namedtuple('poin ...

  6. Python——collections模块

    collections模块 collections模块在内置数据类型(dict.list.set.tuple)的基础上,还提供了几个额外的数据类型:ChainMap.Counter.deque.def ...

  7. python collections模块详解

    参考老顽童博客,他写的很详细,例子也很容易操作和理解. 1.模块简介 collections包含了一些特殊的容器,针对Python内置的容器,例如list.dict.set和tuple,提供了另一种选 ...

  8. Python——collections模块(扩展数据类型)

    1.namedtuple:利用坐标.空间坐标,扑克牌等指定空间位置 # namedtuple('名字',[list列表属性])from collections import namedtuple Po ...

  9. python collections模块 之 defaultdict

    defaultdict 是 dict 的子类,因此 defaultdict 也可被当成 dict 来使用,dict 支持的功能,defaultdict 基本都支持.但它与 dict 最大的区别在于,如 ...

随机推荐

  1. 从Hadoop+Storm架构转向Spark架构

  2. mysql 复制原理详解

    http://www.cnblogs.com/kristain/articles/4142970.html

  3. CSIC_716_20191112【闭包函数和装饰器】

    闭包函数 什么是闭包函数:闭包函数是函数嵌套.函数对象.名称空间和作用域的集合体. 闭包函数必须在函数内部定义,闭包函数可以引用外层函数的名字. # _*_ coding: gbk _*_ # @Au ...

  4. layui中的tab切换

    tab切换是常见的效果,为了方便经常使用插架中自带的,下面是layui中自带的tab切换效果, 主要代码如下: <!DOCTYPE html> <html lang="en ...

  5. leetcode-第10周双周赛-5081-歩进数

    题目描述: 自己的提交:参考全排列 class Solution: def countSteppingNumbers(self, low: int, high: int) -> List[int ...

  6. PHP MVC运用

    php中的MVC模式运用 首先我来举个例子: 一个简单的文章显示系统 简单期间,我们假定这个文章系统是只读的,也就是说这个例子将不涉及文章的发布,现在开始了. 由于只涉及数据库的读取,所以我定义了两个 ...

  7. spring boot项目开发中遇到问题,持续更新

    1.JPA中EntityManager不能执行建表语句,提示要加事务Error:javax.persistence.TransactionRequiredException: Executing an ...

  8. (转)Android中RelativeLayout各个属性的含义

    转:http://blog.csdn.net/softkexin/article/details/5933589 android:layout_above="@id/xxx"  - ...

  9. hexo 错误汇总

    文章目录 发布文章遇到: 发布文章的时候出现错误: 代码推送到github,hexo g -d 半天推送不上去 记录一次hexo+coding hexo s本都没问题,hexo g -d 样式并未改变 ...

  10. Python 函数与内置函数

    1.函数的基本定义 def 函数名称(参数) 执行语句 return 返回值 def : 定义函数的关键字: 函数名称:顾名思义,就是函数的名字,可以用来调用函数,不能使用关键字来命名,做好是用这个函 ...