转于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. JVM类加载流程

    1.加载 a.装载类的第一个阶段 b.取得类的二进制流 c.转为方法区数据结构 d.在Java堆中生成对应的java.lang.Class对象 2.链接 a.验证(保证Class流的格式是正确的) 文 ...

  2. jQuery实现复选框全选/所有取消/反选/获得选择的值

    <!DOCTYPE html> <html> <head> <script type="text/javascript" src=&quo ...

  3. Openstack 架构简述

    概述 在学习OpenStack的过程中,感觉对整个OpenStack的架构稍稍有些了解,所以将这些记录下来,一来防止自己忘记,二来也可以对有需要的人提供帮助 本文章相关的灵感/说明/图片来自于http ...

  4. RLearning第2弹:创建数据集

    任何一门语言,数据类型和数据结构是最基础,也是最重要的,必须要学好!1.产生向量 a<-c(1,2,5,3,6,-2,4) b<-c("one","two&q ...

  5. c++学习笔记(网上资料)

                                    C++笔记       2007-3-22 1. 程序 —— 可执行文件,人发送给计算机的一组指令.         硬件指令是二进制, ...

  6. Eclipse输入命令行参数

    想要在Eclipse中输入命令行参数,可以在目录中该程序上右键,选择“Run As",选择”Run configurations",如图: 然后输入命令行参数: 点击Apply和R ...

  7. UIView的概念与使用

    什么是UIView UIView可以称之为控件/视图 屏幕上所有能看到的东西都是UIView 按钮(UIButton).文本(UILabel)都是控件 控件都有一些共同的属性 -- 尺寸 -- 位置 ...

  8. POJ 1068 Parencodings (类似括号的处理问题)

                                                                                                    Pare ...

  9. A股牛蹄板块轮动迹象

    2018年10月熊市末期牛市初期 1.券商 2.中小银行 3.稀土永磁 4.铁路基建 5.人工智能 6.电器仪表 7.保险 8.非进金融.供热供气

  10. CentOS下查看文件和文件夹大小

    当磁盘大小超过标准时会有报警提示,这时如果掌握df和du命令是非常明智的选择. df可以查看一级文件夹大小.使用比例.档案系统及其挂入点,但对文件却无能为力. 当磁盘大小超过标准时会有报警提示,这时如 ...