队列:队列是先进先出。
import queue q = queue.Queue() q.put(1) q.put(2) q.put(3) q.put(4) print(q.get()) print(q.get()) print(q.get()) print(q.get())
栈:栈是先进后出。
import queue q = queue.LifoQueue() q.put(1) q.put(2) q.put(3) q.put(4) print(q.get()) print(q.get()) print(q.get()) print(q.get())
优先级队列:优先队列是根据优先级判定谁先出来,如果优先级一样,则按数据的ascii码输出。
import queue q = queue.PriorityQueue() q.put((10, ‘b‘)) q.put((30, ‘a‘)) q.put((13, ‘f‘)) q.put((-3, ‘k‘)) q.put((-3.5, ‘c‘)) q.put((-7.5, ‘h‘)) print(q.get()) print(q.get()) print(q.get()) print(q.get()) print(q.get()) print(q.get())
输出结果:
(-7.5, ‘h‘) (-3.5, ‘c‘) (-3, ‘k‘) (10, ‘b‘) (13, ‘f‘) (30, ‘a‘)
数越小越优先
优先级一样,比较后面数据的ascii码
原文地址:https://www.cnblogs.com/aaronthon/p/9863095.html
时间: 2024-11-05 21:57:23