python线程中deque怎么使用

小编给大家分享一下python线程中deque怎么使用,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

python有哪些常用库

python常用的库:1.requesuts;2.scrapy;3.pillow;4.twisted;5.numpy;6.matplotlib;7.pygama;8.ipyhton等。

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怎么使用”有了一定的了解,如果想了解更多相关知识,欢迎关注行业资讯频道,感谢各位的阅读!