首页 > 编程语言 > 详细

python 列表之队列

时间:2017-08-14 16:09:13      阅读:216      评论:0      收藏:0      [点我收藏+]

列表实现队列操作(FIFO),可以使用标准库里的 collections.deque,deque是double-ended quene的缩写,双端队列的意思,它可以实现从队列头部快速增加和取出对象。

>>> from collections import deque
>>> queue = deque(["Eric", "John", "Michael"])
>>> queue.append("Terry")           # Terry arrives
>>> queue.append("Graham")          # Graham arrives
>>> queue.popleft()                 # The first to arrive now leaves
Eric
>>> queue.popleft()                 # The second to arrive now leaves
John
>>> queue                           # Remaining queue in order of arrival
deque([Michael, Terry, Graham])

deque用rotate实现跑马灯操作,转自http://www.zlovezl.cn/articles/collections-in-python/

# -*- coding: utf-8 -*-
"""
下面这个是一个有趣的例子,主要使用了deque的rotate方法来实现了一个无限循环
的加载动画
"""
import sys
import time
from collections import deque

fancy_loading = deque(‘>--------------------‘)

while True:
    print ‘\r%s‘ % ‘‘.join(fancy_loading),
    fancy_loading.rotate(1)
    sys.stdout.flush()
    time.sleep(0.08)

  

python 列表之队列

原文:http://www.cnblogs.com/guoxueyuan/p/7358061.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!