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. 关于RSA加密和签名的区别与联系

    发现网上对于RSA加密和签名的介绍普遍偏向于使用和概念的说明,今天想说一点不一样的.对于加解密和签名的使用及概念就不再说了,不知道的请自行百度. 签名的本质其实就是加密,但是由于签名无需还原成明文,因 ...

  2. Tesseract&tesseractOCRiOS

    安装tesseract在上篇. 1.安装之后默认语言包只有英文包,在github上下载中文简体,链接:https://github.com/tesseract-ocr/tessdata 然后放入tes ...

  3. linux下svn 客户端使用方式

    输入 yes 开始 checkout服务器上的文件到本地目录 2.将文件 添加文件到某个目录下(是svn的服务器checkout下来的目录中) 3. 提交到服务器 4 .即可在服务器目录看到(wind ...

  4. python中logging使用方法

    1.logging提供了一组便利的函数,用来做简单的日志.它们是 debug(). info(). warning(). error() 和 critical(). 1.1logging以严重程度递增 ...

  5. LeetCode动态规划题总结【持续更新】

    以下题号均为LeetCode题号,便于查看原题. 10. Regular Expression Matching 题意:实现字符串的正则匹配,包含'.' 和 '*'.'.' 匹配任意一个字符,&quo ...

  6. sql中取出字符串中数字

    select substring(reverse('0->星光'),PATINDEX('%[0-9]%',reverse('0->星光')),1)

  7. Cats Transport

    Cats Transport 现在有n座山,第i座山的坐标为\(d_i\),初始p个饲养员在山1,有m只猫,每只猫有一个属性\(h_i,t_i\)表示猫i 在\(h_i\)以及它在\(t_i\)时间后 ...

  8. Kunbernetes从私有仓库nexus拉取镜像

    1.docker登陆认证 [root@master ~]# vim /etc/docker/daemon.json { "insecure-registries": [" ...

  9. mysql中的字符串截取和替换

    -- 替换 replace(字段名,"需要替换的字符","替换的字符") mysql里replace不支持正则匹配 mysql> set @needRep ...

  10. Day 6:集合(set)

    集合(set)定义:由不同元素组成的集合,集合中是一组无序排列的可hash值,可以作为字典的key特性:集合里面数据类型是不可变的1.可变的数据类型:列表,字典2.不可变的数据类型:数字.元组.字符串 ...