queue for python code example
Example 1: queue python
from queue import Queue
q = Queue()
q.size()
q.empty()
q.put(item)
q.get()
Example 2: python list as queue
queue = []
queue.append('a')
queue.append('b')
print(queue)
print("\nElements dequeued from queue")
print(queue.pop(0))
print(queue.pop(0))
print("\nQueue after removing elements")
print(queue)
Example 3: python queue.priority queue
from queue import PriorityQueue
class PqElement(object):
def __init__(self, value: int):
self.val = value
def __lt__(self, other):
"""self < obj."""
return self.val > other.val
def __repr__(self):
return f'PQE:{self.val}'
pq = PriorityQueue()
pq.put(PqElement(v))
topValue = pq.get()
topValue = pq.queue[0].val