前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python collections 模块中的 deque(双端队列)

python collections 模块中的 deque(双端队列)

作者头像
用户7886150
修改2020-12-24 10:21:10
4850
修改2020-12-24 10:21:10
举报
文章被收录于专栏:bit哲学院bit哲学院

参考链接: Python中的双端队列DeQue

deque

 1、概述2、相关操作3、知识点

1、概述 

 deque结构可以看作是内置的list结构的加强版,且比队列提供了更强大的方法。  deque 是 double-ended queue的缩写,类似于 list,与list不同的是,它提供了在两端插入和删除的操作。  简单来说,deque可以看做是一个双向列表,左右两端都可以进行操作  

2、相关操作 

 append() 和普通的列表append方法一样 “”" Add an element to the right side of the deque. “”"  

from collections import deque

d1 = deque()

d1.append('a')

d1.append('b')

d1.append('c')

print(d1)

#输出结果>>>: 

    #deque(['a', 'b', 'c'])

 appendleft() 和append作用是一样的,只不过是向双端队列左端即头部添加值 “”" Add an element to the left side of the deque. “”"  

from collections import deque

d1 = deque()

d1.appendleft('a')

d1.appendleft('b')

d1.appendleft('c')

print(d1)

#输出结果>>>: 

    #deque(['c', 'b', 'a'])

 clear() 清空当前双端队列 “”" Remove all elements from the deque. “”"  

from collections import deque

d1 = deque()

d1.appendleft('a')

d1.appendleft('b')

d1.appendleft('c')

d1.clear()

print(d1)

#输出结果>>>: 

    #deque([])

 copy() 浅拷贝当前双端队列 “”" Return a shallow copy of a deque. “”"  

from collections import deque

d1 = deque()

d1.appendleft('a')

d1.appendleft('b')

d1.appendleft('c')

d2 = d1.copy()

print(d1)

print(d2)

#输出结果>>>: 

    #deque(['c', 'b', 'a'])

    #deque(['c', 'b', 'a']) 

 count(value) 计算value在当前双端队列中的总个数 “”" D.count(value) -> integer – return number of occurrences of value “”"  

from collections import deque

d1 = deque()

d1.appendleft('a')

d1.appendleft('b')

d1.appendleft('c')

d1.appendleft('a')

d1.appendleft('a')

print(d1)

print(d1.count('a'))

#输出结果>>>:

    #deque(['a', 'a', 'c', 'b', 'a'])

    #3

 extend() 和列表的extend使用方式一样,使用可迭代对象扩展当前双端队列(向右端扩展) “”" Extend the right side of the deque with elements from the iterable “”"  

from collections import deque

d1 = deque()

d1.appendleft('a')

d1.appendleft('b')

d1.appendleft('c')

d1.extend([1,2,3])

print(d1)

#输出结果>>>:

    #deque(['c', 'b', 'a', 1, 2, 3])

 extendleft() 与上方extendleft一样,只不过是向左端扩展 “”" Extend the left side of the deque with elements from the iterable “”"  

from collections import deque

d1 = deque()

d1.appendleft('a')

d1.appendleft('b')

d1.appendleft('c')

d1.extendleft([1,2,3])

print(d1)

#输出结果>>>:

    #deque([3, 2, 1, 'c', 'b', 'a'])

 index(value, start=None, stop=None) 查询value第一次出现的索引值,可以指定索引起始位置和结束位置 “”" D.index(value, [start, [stop]]) -> integer – return first index of value. Raises ValueError if the value is not present. “”"  

from collections import deque

d1 = deque()

d1.appendleft('a')

d1.appendleft('b')

d1.appendleft('c')

d1.extendleft([1,2,3])

print(d1)

print(d1.index(1))

#输出结果>>>:

    #deque([3, 2, 1, 'c', 'b', 'a'])

    #2

 insert(index, p_object) 在索引值为index的前面插入值 “”" D.insert(index, object) – insert object before index “”"  

from collections import deque

d1 = deque()

d1.appendleft('a')

d1.appendleft('b')

d1.appendleft('c')

d1.extendleft([1,2,3])

d1.insert(1,[5,6,7])

print(d1)

#输出结果>>>:

    #deque([3, [5, 6, 7], 2, 1, 'c', 'b', 'a'])

 pop() 从双端队列中删除一个值,并将该值作为返回值返回。(从右端) “”" Remove and return the rightmost element. “”"  

from collections import deque

d1 = deque()

d1.appendleft('a')

d1.appendleft('b')

d1.appendleft('c')

d1.extendleft([1,2,3])

print(d1)

d2 = d1.pop()

print(d1)

print(d2)

#输出结果>>>:

    #deque([3, 2, 1, 'c', 'b', 'a'])

    #deque([3, 2, 1, 'c', 'b'])

    #a

 popleft() 与pop相同,区别是该方法从双端队列左端弹出值 “”" Remove and return the leftmost element. “”"  

from collections import deque

d1 = deque()

d1.appendleft('a')

d1.appendleft('b')

d1.appendleft('c')

d1.extendleft([1,2,3])

print(d1)

d2 = d1.popleft()

print(d1)

print(d2)

#输出结果>>>:

    #deque([3, 2, 1, 'c', 'b', 'a'])

    #deque([2, 1, 'c', 'b', 'a'])

    #3

 remove(value) 从双端队列中删除value元素 “”" D.remove(value) – remove first occurrence of value. “”"  

from collections import deque

d1 = deque()

d1.appendleft('a')

d1.appendleft('b')

d1.appendleft('c')

d1.extendleft([1,2,3])

print(d1)

d1.remove(2)

print(d1)

#输出结果>>>:

    #deque([3, 2, 1, 'c', 'b', 'a'])

    #deque([3, 1, 'c', 'b', 'a'])

 reverse() 反转当前双端队列 “”" D.reverse() – reverse IN PLACE “”"  

from collections import deque

d1 = deque()

d1.appendleft('a')

d1.appendleft('b')

d1.appendleft('c')

d1.extendleft([1,2,3])

print(d1)

d1.reverse()

print(d1)

#输出结果>>>:

    #deque([3, 2, 1, 'c', 'b', 'a'])

    #deque(['a', 'b', 'c', 1, 2, 3])

 rotate() 这是一个神奇的方法,可以按照任意一个方向(左或右)旋转,而跳过一些元素。 “”" Rotate the deque n steps to the right (default n=1). If n is negative, rotates left. “”"  

eg1 

from collections import deque

d1 = deque()

d1.appendleft('a')

d1.appendleft('b')

d1.appendleft('c')

d1.extendleft([1,2,3])

print(d1)

d1.rotate(2)

print(d1)

#输出结果>>>:

    #deque([3, 2, 1, 'c', 'b', 'a'])

    #deque(['b', 'a', 3, 2, 1, 'c'])

eg2: 

rotate支持传入负数 

from collections import deque

d1 = deque()

d1.appendleft('a')

d1.appendleft('b')

d1.appendleft('c')

d1.extendleft([1,2,3])

print(d1)

d1.rotate(-2)

print(d1)

#输出结果>>>:

    #deque([3, 2, 1, 'c', 'b', 'a'])

    #deque([1, 'c', 'b', 'a', 3, 2])

3、知识点 

双端队列是线程安全的,可以在不同的线程中同时从两端利用队列的内容。也就是说不需要自己添加锁,数据就是线程安全的。要充分利用双端队列这一特性。deque在生成双端队列时,可以指定maxlen值,如果队列内的数据量等于maxlen的时候,再插入数据时会把最老的数据从双端队列中剔除掉. 

参考资料: https://blog.csdn.net/qq_33404767/article/details/82691520

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档