Python之Queue模块

昨天在刷leetcode,用到DFS和BFS,所以对于Queue进行记录。

主要实现了三类队列:先进先出、后进先出(类似于队列)、按照优先级输出

队列的方法:Queue.put()、Queue.get()、Queue.empty()、Queue.full()、Queue.qsize()

1、先进先出

In [1]: from queue import Queue

In [2]: from queue import LifoQueue

In [3]: from queue import PriorityQueue

In [4]: q1 = Queue()

In [5]: for i in range(6):
   ...:     q1.put(i)
   ...:

In [6]: q1.qsize()
Out[6]: 6

In [7]: q1.full()
Out[7]: False

In [8]: q1.empty()
Out[8]: False

In [11]: while not q1.empty():
    ...:     print(q1.get(),end=' ')
    ...:
    ...:
0 1 2 3 4 5

2、先进后出

In [12]: q2 = LifoQueue()

In [14]: for i in range(6):
    ...:     q2.put(i)
    ...:
    ...:

In [15]: while not q2.empty():
    ...:     print(q2.get(),end=' ')
    ...:
    ...:
5 4 3 2 1 0

3、按照优先级输出

In [16]: q3 = PriorityQueue()

In [17]: m = [(1,'s'),(3,'h'),(2,'m')]

In [18]: for i in range(len(m)):
    ...:     q3.put(m[i])
    ...:

In [19]: while not q3.empty():
    ...:     print(q3.get(),end=' ')
    ...:
(1, 's') (2, 'm') (3, 'h')

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值