python线程中deque如何使用?

2025-12-01 0 62,390

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学习网。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:以上部本文内容由互联网用户自发贡献,本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。投诉邮箱:3758217903@qq.com

ZhiUp资源网 python基础 python线程中deque如何使用? https://www.zhiup.top/2315.html

相关