class Foo:
def __init__(self):
self.lst = []
def _put(self, item):
self.lst.append(item)
def _get(self):
return self.lst.pop()
class Queue(Foo):
def get(self):
return self.lst.pop(0)
class Stack(Foo):
pass
import queue
q = queue.Queue(3) # 队列 可设置最多放几个值 用途:处理任务等
q.put(1)
q.put(2)
q.put(3)
try: # 如果超过最大量,做异常处理后会丢失该次put内容
q.put_nowait(4) # 一般不用put异常
except queue.Full:
pass
print(q.get())
print(q.get())
print(q.get())
try: # 判断是否取空的异常判断
q.get_nowait()
except queue.Empty:
pass
from queue import LifoQueue
from queue import Empty,Full
q = LifoQueue(3) # 栈 可设置最多放几个值 用途:算法等
q.put(1)
q.put(2)
q.put(3)
try: # 如果超过最大量,做异常处理后会丢失该次put内容
q.put_nowait(4) # 一般不用put异常
except Full:
print("没人来取,超出最大放置数")
print(q.get())
print(q.get())
print(q.get())
try: # 判断是否取空的异常判断
q.get_nowait()
except Empty:
pass
from queue import PriorityQueue
q = PriorityQueue() # 优先级
q.put((3, "白嫖党"))
q.put((1, "SSVIP"))
q.put((2, "VIP"))
# 按照排序取值,排序是按照ASCII码大小顺序决定
print(q.get()) # (1, 'SSVIP')
print(q.get()) # (2, 'VIP')
print(q.get()) # (2, '白嫖党')