python collections 模块 之 deque
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的更多相关文章
- Python collections模块总结
Python collections模块总结 除了我们使用的那些基础的数据结构,还有包括其它的一些模块提供的数据结构,有时甚至比基础的数据结构还要好用. collections ChainMap 这是 ...
- (转)python collections模块详解
python collections模块详解 原文:http://www.cnblogs.com/dahu-daqing/p/7040490.html 1.模块简介 collections包含了一些特 ...
- python collections模块
collections模块基本介绍 collections在通用的容器dict,list,set和tuple之上提供了几个可选的数据类型 namedtuple() factory function f ...
- Python collections 模块用法举例
Python作为一个“内置电池”的编程语言,标准库里面拥有非常多好用的模块.比如今天想给大家 介绍的 collections 就是一个非常好的例子. 1.collections模块基本介绍 我们都知道 ...
- Python——collections模块、time模块、random模块、os模块、sys模块
1. collections模块 (1)namedtuple # (1)点的坐标 from collections import namedtuple Point = namedtuple('poin ...
- Python——collections模块
collections模块 collections模块在内置数据类型(dict.list.set.tuple)的基础上,还提供了几个额外的数据类型:ChainMap.Counter.deque.def ...
- python collections模块详解
参考老顽童博客,他写的很详细,例子也很容易操作和理解. 1.模块简介 collections包含了一些特殊的容器,针对Python内置的容器,例如list.dict.set和tuple,提供了另一种选 ...
- Python——collections模块(扩展数据类型)
1.namedtuple:利用坐标.空间坐标,扑克牌等指定空间位置 # namedtuple('名字',[list列表属性])from collections import namedtuple Po ...
- python collections模块 之 defaultdict
defaultdict 是 dict 的子类,因此 defaultdict 也可被当成 dict 来使用,dict 支持的功能,defaultdict 基本都支持.但它与 dict 最大的区别在于,如 ...
随机推荐
- shell 命令 用户管理
1. 查看保存用户相关信息的文件 [ cat /etc/passwd ] [linux : x : 1000 : 1000 : linux,,, : ...
- http协议 头部字段 referrer
学习笔记,非原创,抄自:https://www.cnblogs.com/amyzhu/p/9716493.html:https://blog.csdn.net/java_zhangshuai/arti ...
- 继承内部类时使用外部类对象.super()调用内部类的构造方法
问题简介 今天在看<Java编程思想>的时候,看到了一个很特殊的语法,懵逼了半天--一个派生类继承自一个内部类,想要创建这个派生类的对象,首先得创建其父类的对象,也就是这个内部类,而调 ...
- sql server 的存储过程
存储过程说白了就是一堆 SQL 的合并.中间加了点逻辑控制. 存储过程运行流程 创建不带参数存储过程 --创建存储过程 if (exists (select * from sys.objects wh ...
- [190308]Ubuntu 安装完之后,安装的软件小记
install software vim sudo apt-get install -y vim Typora command copy from Typora website # or run: # ...
- 如何理解 if __name__ == "__main__"
小明.py 朋友眼中你是小明(__name__ == '小明'), 你自己眼中你是你自己(__name__ == '__main__'), 你编程很好, 朋友调你去帮他写程序(import 小明, 这 ...
- Erlang学习记录:运算符
数学运算符 说明 详细 符号 加减乘 +-* 浮点数除 结果为浮点数 / 整数除 除数和被除数都必须是整数,结果为整数 div 整数取余 rem 逻辑运算符 说明 符号 详细 and 前后两个值都为真 ...
- Java 基础 - 如何重写equals()
ref:https://www.cnblogs.com/TinyWalker/p/4834685.html -------------------- 编写equals方法的建议: 显示参数命名为oth ...
- [JZOJ3691] 【CF414E】Mashmokh's Designed tree
题目 题目大意 给你一棵树,接下来对这棵树进行三种操作: 1.询问两点之间的距离. 2.让某个点变为它原来的第\(h\)个祖先的最后一个儿子. 3.求\(dfs\)序中最后一个深度为\(k\)的点. ...
- SQL 标量函数-----日期函数 day() 、month()、year() 转载
select day(createtime) from life_unite_product --取时间字段的天值 select month(createtime) from life_u ...