Python——deque

deque包含在collections模块中,主要包含的方法:append、appendleft、clear、copy、count、extend、extendleft、index、insert、pop、popleft、remove、reverse、rotate等,通过help(deque)可以查看相应的使用说明。

队列的定义和使用:

>>> from collections import deque
>>> q=deque(range(10))
>>> q
deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> 

队列追加元素:

>>> q.append(10)
>>> q
deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
>>> q.appendleft(-1)
>>> q
deque([-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
>>> 

队列出队操作:

>>> q.pop()
10
>>> q
deque([-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> q.popleft()
-1
>>> q
deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> 

移动队列元素:

>>> q
deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> q.rotate()
>>> q
deque([9, 0, 1, 2, 3, 4, 5, 6, 7, 8])
>>> q.rotate(2)
>>> q
deque([7, 8, 9, 0, 1, 2, 3, 4, 5, 6])
>>> q.rotate(-3)
>>> q
deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> 

注:rotate(n)默认情况下是将队尾的一个元素移到队首,若n大于0,则是将队尾的n个元素移到队首,若n小于0,则是将队首的n个元素移动到对尾

获取队列元素下标:

>>> q.append(-1)
>>> q
deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1])
>>> q.index(-1)
10
>>> 
### Python 中 `deque` 的使用方法及示例 #### 定义与初始化 `deque` 是 Python 标准库 `collections` 模块中的一个类,表示双端队列。这种数据结构允许从两端高效地添加或移除元素[^1]。 ```python from collections import deque # 创建一个空的 deque 对象 d = deque() # 初始化带有初始元素的 deque initial_elements = [1, 2, 3, 4] dq_with_initial_values = deque(initial_elements) print(dq_with_initial_values) # 输出: deque([1, 2, 3, 4]) ``` #### 基本操作 - **添加元素** - 可以在左侧(`appendleft`) 或右侧 (`append`) 添加单个元素。 ```python d.append(5) # 在右边追加元素 d.appendleft(-1) # 在左边追加元素 print(d) # 如果之前为空,则输出: deque([-1, 5]) ``` - **移除元素** - 同样可以从两侧移除元素,分别使用 `pop()` 和 `popleft()` 方法。 ```python last_element = d.pop() # 移除并返回最右端元素 first_element = d.popleft() # 移除并返回最左端元素 print(last_element) # 输出最后一个被弹出的元素值 print(first_element) # 输出第一个被弹出的元素值 ``` - **旋转元素** - 调用 `rotate(n)` 函数可以让所有成员沿指定方向移动 n 步;正数代表向右转,负数则相反。 ```python dq_to_rotate = deque([1, 2, 3, 4]) dq_to_rotate.rotate(1) print(dq_to_rotate) # 输出: deque([4, 1, 2, 3]) ``` #### 应用场景实例——生产者-消费者模式下的线程安全队列 为了确保多线程环境下对共享资源的安全访问,在创建基于 `deque` 的生产者-消费者模型时引入了锁机制: ```python import threading from collections import deque class ProducerConsumerQueue: def __init__(self, maxsize=0): """ 构造函数设置最大长度,默认无界。 参数: maxsize (int): 队列的最大容量,当设为非零整数值时表示有界的队列; 设定为0意味着不受限。 """ self._queue = deque(maxlen=maxsize) self._lock = threading.Lock() def put(self, item): """生产者调用此方法往队列里放入新条目""" with self._lock: self._queue.append(item) def get(self): """消费者获取下一个可用项的方法""" with self._lock: return self._queue.popleft() if self._queue else None def size(self): """查询当前队列内有多少个项目等待处理""" return len(self._queue) if __name__ == "__main__": queue_instance = ProducerConsumerQueue(maxsize=5) producer_thread = threading.Thread(target=lambda q, i: q.put(i), args=(queue_instance,)) consumer_thread = threading.Thread(target=lambda q: print(q.get()), args=(queue_instance,)) producer_thread.start() consumer_thread.start() producer_thread.join() consumer_thread.join() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值