转于https://www.cnblogs.com/zhenwei66/p/6598996.html

博主http://www.cnblogs.com/zhenwei66/(渐晨)

python3 deque(双向队列)

创建双向队列

import collections
d = collections.deque()

append(往右边添加一个元素)

import collections
d = collections.deque()
d.append(1)
d.append(2)
print(d) #输出:deque([1, 2])

appendleft(往左边添加一个元素)

import collections
d = collections.deque()
d.append(1)
d.appendleft(2)
print(d) #输出:deque([2, 1])

clear(清空队列)

import collections
d = collections.deque()
d.append(1)
d.clear()
print(d) #输出:deque([])

copy(浅拷贝)

import collections
d = collections.deque()
d.append(1)
new_d = d.copy()
print(new_d) #输出:deque([1])

count(返回指定元素的出现次数)

import collections
d = collections.deque()
d.append(1)
d.append(1)
print(d.count(1)) #输出:2

extend(从队列右边扩展一个列表的元素)

import collections
d = collections.deque()
d.append(1)
d.extend([3,4,5])
print(d) #输出:deque([1, 3, 4, 5])

extendleft(从队列左边扩展一个列表的元素)

import collections
d = collections.deque()
d.append(1)
d.extendleft([3,4,5])
print(d)
#
# #输出:deque([5, 4, 3, 1])

index(查找某个元素的索引位置)

import collections
d = collections.deque()
d.extend(['a','b','c','d','e'])
print(d)
print(d.index('e'))
print(d.index('c',0,3)) #指定查找区间 #输出:deque(['a', 'b', 'c', 'd', 'e'])
# 4
# 2

insert(在指定位置插入元素)

import collections
d = collections.deque()
d.extend(['a','b','c','d','e'])
d.insert(2,'z')
print(d) #输出:deque(['a', 'b', 'z', 'c', 'd', 'e'])

pop(获取最右边一个元素,并在队列中删除)

import collections
d = collections.deque()
d.extend(['a','b','c','d','e'])
x = d.pop()
print(x,d) #输出:e deque(['a', 'b', 'c', 'd'])

popleft(获取最左边一个元素,并在队列中删除)

import collections
d = collections.deque()
d.extend(['a','b','c','d','e'])
x = d.popleft()
print(x,d) #输出:a deque(['b', 'c', 'd', 'e'])

remove(删除指定元素)

import collections
d = collections.deque()
d.extend(['a','b','c','d','e'])
d.remove('c')
print(d) #输出:deque(['a', 'b', 'd', 'e'])

reverse(队列反转)

import collections
d = collections.deque()
d.extend(['a','b','c','d','e'])
d.reverse()
print(d) #输出:deque(['e', 'd', 'c', 'b', 'a'])

rotate(把右边元素放到左边)

import collections
d = collections.deque()
d.extend(['a','b','c','d','e'])
d.rotate(2) #指定次数,默认1次
print(d) #输出:deque(['d', 'e', 'a', 'b', 'c'])

Python:collections的deque()方法的更多相关文章

  1. python 全栈开发,Day26(hashlib文件一致性,configparser,logging,collections模块,deque,OrderedDict)

    一.hashlib文件一致性校验 为何要进行文件一致性校验? 为了确保你得到的文件是正确的版本,而没有被注入病毒和木马程序.例如我们经常在网上下载软件,而这些软件已经被注入了一些广告和病毒等,如果不进 ...

  2. python collections deque

    collections是python的高级容器类库,包含了dict.truple之外的常用容器. 下面介绍常用的deque 1. deque是双端队列,可以从两端塞元素进去,也可以从两端取元素. 2. ...

  3. python collections 模块 之 deque

    class collections.deque(iterable[,maxlen]): 返回 由可迭代对象初始化的 从左向右的 deque 对象. maxlen: deque 的最大长度,一旦长度超出 ...

  4. 739. Daily Temperatures && 单调栈 && Python collections deque

    题目大意 给你接下来每一天的气温,求出对于每一天的气温,下一次出现比它高气温的日期距现在要等多少天 解题思路 利用单调栈,维护一个单调递减的栈 将每一天的下标i入栈,维护一个温度递减的下标 若下一个温 ...

  5. Python collections 模块用法举例

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

  6. Python collections模块总结

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

  7. python collections模块详解

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

  8. (转)python collections模块详解

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

  9. Python Collections详解

    Python Collections详解 collections模块在内置数据结构(list.tuple.dict.set)的基础上,提供了几个额外的数据结构:ChainMap.Counter.deq ...

随机推荐

  1. 2017-2018-1 20179209《Linux内核原理与分析》第十周作业

    设备与模块 设备分类 块设备 块设备可以以块为单位寻址,块大小随设备不同而不同:设备通常支持重定位操作,也就是对数据的随机访问.块设备的例子有外存,光盘等. 字符设备 字符设备不可寻址,仅供数据的流式 ...

  2. 在线工具集合(新增cron quartz表达式在线生成……)

    缘起 平时工作,须要一些工具.经过一些使用,对照,保留一些比較方便好用的在线工具 工具会持续更新中.. . 在线编译&&反编译  http://www.showmycode.com/ ...

  3. json遍历

    $.each(item.attributes,function (name,value) { });

  4. eclipse安装Activiti Designer插件(转载:http://blog.csdn.net/qq_33547950/article/details/54926435)

    为了完成毕业设计,需要学习Activiti.万事开头难,果然刚开始就遇到了问题.<Activiti实战>和视频教程里提供的安装Activiti Designer插件方法(即下文方法一)不能 ...

  5. ARDUINO MEGA2560 经过ESP8266 WIFI模块上传温湿度数据到 OneNet 服务器

    简述 原来写了一个C++的wifi库但是发现用c++ arduino这小身板有点扛不住,代码比较大,使用String类型数据处理速度慢,而且很容易无缘无故跑飞.而且封装成库后使用还需要修改arduin ...

  6. SpringBoot学习笔记(3):静态资源处理

    SpringBoot学习笔记(3):静态资源处理 在web开发中,静态资源的访问是必不可少的,如:Html.图片.js.css 等资源的访问. Spring Boot 对静态资源访问提供了很好的支持, ...

  7. 自定义jsonp请求数据

    整理代码的时候发现一个以前写的实现jsonp请求方法,放在这里分享一下~ 原理:通过js新建script dom对象,利用src携带参数和callback方法,将数据发送至后端,需要后端配合将数据放在 ...

  8. HDU - 1114 Piggy-Bank 【完全背包】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1114 题意 给出一个储钱罐 不知道里面有多少钱 但是可以通过重量来判断 先给出空储钱罐的重量 再给出装 ...

  9. action extension添加图标

    最近在做ios的action extension,这里记录一下添加图标的方法. 在Action Extension的target里面的Build Settings,里面的Asset Catalog C ...

  10. grep egrep

    grep: Global search REgular expression and Print out the line. 作用: 文本搜索工具,根据用户指定的“模式”对目标文本逐行进行匹配检查:打 ...