Python:collections的deque()方法
转于: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()方法的更多相关文章
- python 全栈开发,Day26(hashlib文件一致性,configparser,logging,collections模块,deque,OrderedDict)
一.hashlib文件一致性校验 为何要进行文件一致性校验? 为了确保你得到的文件是正确的版本,而没有被注入病毒和木马程序.例如我们经常在网上下载软件,而这些软件已经被注入了一些广告和病毒等,如果不进 ...
- python collections deque
collections是python的高级容器类库,包含了dict.truple之外的常用容器. 下面介绍常用的deque 1. deque是双端队列,可以从两端塞元素进去,也可以从两端取元素. 2. ...
- python collections 模块 之 deque
class collections.deque(iterable[,maxlen]): 返回 由可迭代对象初始化的 从左向右的 deque 对象. maxlen: deque 的最大长度,一旦长度超出 ...
- 739. Daily Temperatures && 单调栈 && Python collections deque
题目大意 给你接下来每一天的气温,求出对于每一天的气温,下一次出现比它高气温的日期距现在要等多少天 解题思路 利用单调栈,维护一个单调递减的栈 将每一天的下标i入栈,维护一个温度递减的下标 若下一个温 ...
- Python collections 模块用法举例
Python作为一个“内置电池”的编程语言,标准库里面拥有非常多好用的模块.比如今天想给大家 介绍的 collections 就是一个非常好的例子. 1.collections模块基本介绍 我们都知道 ...
- Python collections模块总结
Python collections模块总结 除了我们使用的那些基础的数据结构,还有包括其它的一些模块提供的数据结构,有时甚至比基础的数据结构还要好用. collections ChainMap 这是 ...
- python collections模块详解
参考老顽童博客,他写的很详细,例子也很容易操作和理解. 1.模块简介 collections包含了一些特殊的容器,针对Python内置的容器,例如list.dict.set和tuple,提供了另一种选 ...
- (转)python collections模块详解
python collections模块详解 原文:http://www.cnblogs.com/dahu-daqing/p/7040490.html 1.模块简介 collections包含了一些特 ...
- Python Collections详解
Python Collections详解 collections模块在内置数据结构(list.tuple.dict.set)的基础上,提供了几个额外的数据结构:ChainMap.Counter.deq ...
随机推荐
- 我的Android进阶之旅------>Android之动画之Frame Animation实例
============================首先看看官网上关于Frame animation的介绍================================ 地址:http://de ...
- 怎样解决KEIL 5 编译KEIL4的带有RTX系统的project解决方法
1.笔者个人对KEIL5与KEIL4的比較 相较于KEIL 5 的"华丽",笔者还是喜欢KEIL4的"内敛".主要也还是习惯了, ...
- Xen虚拟化基础篇
一.xen的简介 Xen是一个开放源代码虚拟机监视器,由剑桥大学开发.它打算在单个计算机上运行多达128个有完全功能的操作系统. 在旧(无虚拟硬件)的处理器上执行Xen,操作系统必须进行显式地修改(& ...
- memcpy使用
void memcpy(void dest, const void *src, size_t n); 功能编辑 从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置 ...
- ubuntu vim退出时出错
E505: "vimrc" is read-only (add ! to override) wq退出时加!强制保存退出 "vimrc" E212: Can't ...
- maven 手动加载第三方jar、zip包
使用maven搭建工程时,难免要加载大量的第三方的jar包.zip包比较少用,而maven的官网提供的jar往往不能满足需求,这时需要我们手动加载到我们本地或nexus私服的仓库中. 1.加载jar包 ...
- 运用starling开发的手游FlappyBird
最近想向游戏方面发展,于是用starling做了一个简易版的FlappyBird,纯AS3开发,权当是技术学习.在发布之后才明白要发布一个没有版权的app是有多困难,审核了N遍之后终于通过审核,下面发 ...
- dhtmlxgrid v3.0学习笔记
dhtmlxgrid v3.0学习笔记 分类: dhtmlx JavaScript2012-01-31 15:41 1744人阅读 评论(0) 收藏 举报 stylesheetdatecalendar ...
- 使用 sqoop 将mysql数据导入到hdfs(import)
Sqoop 将mysql 数据导入到hdfs(import) 1.创建mysql表 CREATE TABLE `sqoop_test` ( `id` ) DEFAULT NULL, `name` va ...
- asp.net ajax实现md5加密
1. [图片] asp.net ajax 效果截图.png 2. [代码]前端代码HTML/Javascript/jQuery <!DOCTYPE html PUBLIC "-//W3 ...