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 最大的区别在于,如 ...
随机推荐
- JS对象 返回/设置时间方法 get/setTime() 返回/设置时间,单位毫秒数 一小时为:60*60*1000
返回/设置时间方法 get/setTime() 返回/设置时间,单位毫秒数,计算从 1970 年 1 月 1 日零时到日期对象所指的日期的毫秒数. 如果将目前日期对象的时间推迟1小时,代码如下: &l ...
- 随笔记录 MBR扇区故障系统备份与还原 2019.8.7
系统备份: [root@localhost ~]# mkdir /abc [root@localhost ~]# mount /dev/sdb1 /abc [root@localhost ~]# dd ...
- 【笔记篇】斜率优化dp(五) USACO08MAR土地购(征)买(用)Land Acquisition
好好的题目连个名字都不统一.. 看到这种最大最小的就先排个序嘛= =以x为第一关键字, y为第二关键字排序. 然后有一些\(x_i<=x_{i+1},且y_i<=y_{i+1}\)的土地就 ...
- All you need to know about: solder mask and paste mask
1, 从字面理解 (1) 从字面理解,solder mask意指要mask住需要solder的地方.那么被mask的是谁呢?是绿油层.可以把默认形态的绿油层想象成与PCB板形状.面积相同,solder ...
- Altera设置Virtual Pin
1,GUI方式 大家都知道的,assignment editor –> category –> logic options –> to –> virtual pin –> ...
- Windows 设置内网和外网同时使用
想要电脑同时使用内网和外网必须具备两个网卡,一个是无线网卡一个是本地连接,无线网卡用来连接wifi也就是外网,而本地连接需要网线连接内网,外网是不需要做设置的,我们只需要设置内网即可,鼠标右击电脑右下 ...
- 各种版本mysql驱动包下载地址
http://central.maven.org/maven2/mysql/mysql-connector-java/
- VS2010-MFC(工具栏:工具栏资源及CToolBar类)
转自:http://www.jizhuomi.com/software/215.html 上一节讲了菜单及CMenu类的使用,这一节讲与菜单有密切联系的工具栏. 工具栏简介 工具栏一般位于主框架窗口的 ...
- VS2010-MFC(常用控件:滚动条控件Scroll Bar)
转自:http://www.jizhuomi.com/software/191.html 滚动条控件简介 滚动条大家也很熟悉了,Windows窗口中很多都有滚动条.前面讲的列表框和组合框设置了相应属性 ...
- VIM 代码自动补全, YouCompleteMe安装及配置
效果 下载 使用Vundle安装 YCM 1. 安装Vundle window用户安装vundle参考这里:Windows下 vundle的安装和使用 2.