Did You Know — Python Has A Built-in Priority Queue
import queue
# Create a priority queue
pq = queue.PriorityQueue()
# Add items to the queue with a priority number
pq.put((1, 'Task with priority 1'))
pq.put((3, 'Task with priority 3'))
pq.put((2, 'Task with priority 2'))
# Retrieve items from the queue
while not pq.empty():
priority, task = pq.get()
print(f'Priority: {priority}, Task: {task}')
#clcoding.com
Priority: 1, Task: Task with priority 1
Priority: 2, Task: Task with priority 2
Priority: 3, Task: Task with priority 3
0 Comments:
Post a Comment