在 Python 中,可以使用 queue.PriorityQueue 或 heapq 模块来实现优先级队列。
方法 1:使用 queue.PriorityQueue
PriorityQueue 是线程安全的,但在某些情况下可能会有性能开销。使用 PriorityQueue 时,需要将每个元素作为 (优先级, 元素) 形式的元组放入队列中,其中“优先级”是一个数字,值越小表示优先级越高。
import queue
# 创建一个优先级队列
pq = queue.PriorityQueue()
# 放入元素 (优先级, 数据)
pq.put((1, "任务1")) # 优先级1
pq.put((3, "任务3")) # 优先级3
pq.put((2, "任务2")) # 优先级2
# 取出元素(按照优先级从高到低的顺序)
while not pq.empty():
priority, task = pq.get()
print(f"优先级: {priority}, 任务: {task}")
方法 2:使用 heapq
heapq 是一个小顶堆,使用它可以实现优先级队列。与 PriorityQueue 类似,每个元素也需要使用 (优先级, 元素) 形式的元组来表示优先级。
import heapq
# 创建一个空列表作为堆
pq = []
# 使用 heappush 添加元素
heapq.heappush(pq, (1, "任务1"))
heapq.heappush(pq, (3, "任务3"))
heapq.heappush(pq, (2, "任务2"))
# 使用 heappop 取出元素(按照优先级从高到低的顺序)
while pq:
priority, task = heapq.heappop(pq)
print(f"优先级: {priority}, 任务: {task}")
两种方法的选择
queue.PriorityQueue:适合多线程环境,因为它是线程安全的。
heapq:适合单线程环境,速度较快。
https://818ps.com/muban/oubosiwang13238710115weitongbu.html
https://818ps.com/muban/oubosiwanghezuo13238710115weitongbu.html
https://818ps.com/muban/oubosiwangbaosha13238710115weitongbu.html
https://818ps.com/muban/oubosiwangcaokong13238710115weitongbu.html
https://818ps.com/muban/oubojiawang13238710115weitongbu.html
https://818ps.com/muban/oubojiawanghezuo13238710115weitongbu.html
https://818ps.com/muban/oubojiawangbaosha13238710115weitongbu.html
https://818ps.com/muban/oubojiawangyibiyi13238710115weitongbu.html
https://818ps.com/muban/oubobaoshawang13238710115weitongbu.html
https://818ps.com/muban/oubobaoshamaifen13238710115weitongbu.html
https://818ps.com/muban/oubobaoshawanghezuo13238710115weitongbu.html
https://818ps.com/muban/oubo1bi113238710115weitongbu.html
https://818ps.com/muban/ouboyibiyi13238710115weitongbu.html
https://818ps.com/muban/oubo1bi1siwang13238710115weitongbu.html
https://818ps.com/muban/oubo1bi1jiawang13238710115weitongbu.html
https://818ps.com/muban/ouboyibiyihezuo13238710115weitongbu.html
https://818ps.com/muban/yaxingsiwang13238710115weitongbu.html
https://818ps.com/muban/yaxingsiwanghezuo13238710115weitongbu.html
https://818ps.com/muban/yaxingsiwangbaosha13238710115weitongbu.html
https://818ps.com/muban/yaxingsiwangcaokong13238710115weitongbu.html
https://818ps.com/muban/yaxingjiawang13238710115weitongbu.html
https://818ps.com/muban/yaxingjiawanghezuo13238710115weitongbu.html
https://818ps.com/muban/yaxingjiawangbaosha13238710115weitongbu.html
https://818ps.com/muban/yaxingjiawangyibiyi13238710115weitongbu.html
https://818ps.com/muban/yaxingbaoshawang13238710115weitongbu.html
https://818ps.com/muban/yaxingbaoshamaifen13238710115weitongbu.html
https://818ps.com/muban/yaxingbaoshawanghezuo13238710115weitongbu.html
https://818ps.com/muban/yaxing1bi113238710115weitongbu.html
https://818ps.com/muban/yaxingyibiyi13238710115weitongbu.html
https://818ps.com/muban/yaxing1bi1siwang13238710115weitongbu.html
https://818ps.com/muban/yaxing1bi1jiawang13238710115weitongbu.html
https://818ps.com/muban/yaxingyibiyihezuo13238710115weitongbu.html
https://818ps.com/muban/huanqiu360siwang13238710115weitongbu.html
https://818ps.com/muban/huanqiu360siwanghezuo13238710115weitongbu.html
https://818ps.com/muban/huanqiu360siwangbaosha13238710115weitongbu.html
https://818ps.com/muban/huanqiu360siwangcaokong13238710115weitongbu.html
https://818ps.com/muban/huanqiu360jiawang13238710115weitongbu.html
https://818ps.com/muban/huanqiu360jiawanghezuo13238710115weitongbu.html
https://818ps.com/muban/huanqiu360jiawangbaosha13238710115weitongbu.html
https://818ps.com/muban/huanqiu360jiawangyibiyi13238710115weitongbu.html
https://818ps.com/muban/huanqiu360baoshawang13238710115weitongbu.html
https://818ps.com/muban/huanqiu360baoshamaifen13238710115weitongbu.html
https://818ps.com/muban/huanqiu360baoshawanghezuo13238710115weitongbu.html
https://818ps.com/muban/huanqiu3601bi113238710115weitongbu.html
https://818ps.com/muban/huanqiu360yibiyi13238710115weitongbu.html
https://818ps.com/muban/huanqiu3601bi1siwang13238710115weitongbu.html
https://818ps.com/muban/huanqiu3601bi1jiawang13238710115weitongbu.html
https://818ps.com/muban/huanqiu360yibiyihezuo13238710115weitongbu.html
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70040412/viewspace-3045406/
,如需转载,请注明出处,否则将追究法律责任。