• 技术文章 >Python技术 >Python基础教程

    python线程中deque如何使用?

    小妮浅浅小妮浅浅2021-02-24 16:28:06原创2392

    1、说明

    collections.deque 类是一种线程安全的数据类型,可以从两端快速添加或删除元素。而且如果你想有一个数据类型来存储“最近使用过的元素”,deque也是一个不错的选择。这是因为在创建新的双向队列时,您可以指定队列的大小。如果队列已满,您也可以从反端删除过期元素,然后在尾端添加新元素。

    2、实例

    In [1]: from collections import deque
     
    In [2]: dq = deque(range(10), maxlen=10)
     
    In [3]: dq
    Out[3]: deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
     
    In [4]: dq.rotate(3)
     
    In [5]: dq
    Out[5]: deque([7, 8, 9, 0, 1, 2, 3, 4, 5, 6])
     
    In [6]: dq.rotate(-4)
     
    In [7]: dq
    Out[7]: deque([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])
     
    In [8]: dq.appendleft(-1)
     
    In [9]: dq
    Out[9]: deque([-1, 1, 2, 3, 4, 5, 6, 7, 8, 9])
     
    In [10]: dq.extend([11, 22, 33])
     
    In [11]: dq
    Out[11]: deque([3, 4, 5, 6, 7, 8, 9, 11, 22, 33])
     
    In [12]: dq.extendleft([10, 20, 30, 40])
     
    In [13]: dq
    Out[13]: deque([40, 30, 20, 10, 3, 4, 5, 6, 7, 8])

    以上就是python线程中deque的使用,希望能对大家有所帮助,更多知识尽在python学习网。

    专题推荐:python deque
    品易云
    上一篇:defaultdict在python中接收调用对象 下一篇:python列表运算详解

    相关文章推荐

    • python中二叉树有哪些类型• python特征生成中字符类型有哪些?• python用生成器实现协程• python greenlet如何交替运行• python中Fearturetools三个基本概念• defaultdict在python中接收调用对象

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网