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

1、概述

  • deque结构可以看作是内置的list结构的加强版,且比队列提供了更强大的方法。

  • deque 是 double-ended queue的缩写,类似于 list,与list不同的是,它提供了在两端插入和删除的操作。

  • 简单来说,deque可以看做是一个双向列表,左右两端都可以进行操作

2、相关操作

  1. 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'])
  1. 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'])
  1. 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([])
  1. 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']) 
  1. 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
  1. 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])
  1. 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'])
  1. 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
  1. 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'])
  1. 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
  1. 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
  1. 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'])
  1. 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])
  1. 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

  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值