队列
定义:import queue
q = queue.Queue()
进队列:
q.put(x)
出队列:
y = q.get()
队列元素个数:
len = q.qsize()
例子:
import queue
q = queue.Queue()
q.put(3)
q.put(1)
q.put(4)
q.put(1)
q.put(5)
q.put(9)
print('len = ', q.qsize())
while not q.empty():
print(q.get())
优先队列
python 优先队列
定义:from queue import PriorityQueue
q = PriorityQueue()
进队列:
q.put([id, val]) id代表优先级,越小优先级越高
出队列:
x = q.get()
队列元素个数:
len = q.qsize()
例子:
from queue import PriorityQueue
q = PriorityQueue()
q.put(3)
q.put(1)
q.put(4)
q.put(1)
q.put(5)
q.put(9)
print('len = ', q.qsize())
while not q.empty():
print(q.get())