【发布时间】:2012-09-14 22:27:41
【问题描述】:
如何在 python 中检查双端队列的长度?
我没有看到他们在 Python 中提供 deque.length...
http://docs.python.org/tutorial/datastructures.html
from collections import deque
queue = deque(["Eric", "John", "Michael"])
如何检查这个双端队列的长度?
我们可以像这样初始化吗
queue = deque([]) #is this length 0 deque?
【问题讨论】:
-
你试过
len(queue)吗?这通常是 Python 处理元素计数的方式。 -
print(len(my_queue.queue))为我工作。 -
我在编辑一个有这么多赞成票的问题时犹豫不决,但我认为在问题标题和正文中使用“队列”以外的词是有道理的,因为许多谷歌搜索有关 python @987654327 @ 将在这里结束,而接受的答案根本不适用于
Queue。 (是的,我知道deque也是一种队列 - 在这种情况下,这只是一个不幸的词......) -
计算
queue.Queue(或multiprocessing.Queue)对象的长度,参考Get length of Queue in Python's multiprocessing library - Stack Overflow
标签: python python-3.x python-2.7 data-structures