d.appendleft(d.pop())
,向左循环一步就等价于 d.append(d.popleft())
。maxlenDeque的最大尺寸,如果没有限定的话就是 Nonedeque 用法① linux下查看最新日志的命令是:tail -n 2 test.log
,deque也可以实现同样的功能
def tail(filename, n=10):with open(filename) as f:return deque(f, n)
② 维护一个近期添加元素的序列,通过从右边添加和从左边弹出
def moving_average(iterable, n=3):# moving_average([40, 30, 50, 46, 39, 44]) --> 40.0 42.0 45.0 43.0# http://en.wikipedia.org/wiki/Moving_averageit = iter(iterable)d = deque(itertools.islice(it, n-1))d.appendleft(0)s = sum(d)for elem in it:s += elem - d.popleft()d.append(elem)yield s / n
【26 python进阶collections标准库】
经验总结扩展阅读
- 信号量 C# 多线程访问之 SemaphoreSlim【C# 进阶】
- 炉石传说新手攻略萌新到底怎么玩(炉石传说新手进阶流程)
- 38 全球名校AI课程库| 马萨诸塞大学 · 自然语言处理进阶课程『Advanced Natural Language Processing』
- Python基础指面向对象:2、动静态方法
- Python基础之面向对象:3、继承与派生
- ysoserial CommonsCollections2 分析
- 如何免安装使用 Python?推荐 17 个在线的 Python 解释器!
- Python处理刚刚,分钟,小时,天前等时间
- python渗透测试入门——取代netcat
- 六 Selenium4+Python3系列 - Selenium的三种等待,强制等待、隐式等待、显式等待