首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Python中检查双端队列长度

如何在Python中检查双端队列长度
EN

Stack Overflow用户
提问于 2012-09-23 07:23:00
回答 3查看 128.4K关注 0票数 60

如何在python中检查deque的长度?

我没看到他们在Python语言中提供deque.length ...

http://docs.python.org/tutorial/datastructures.html

代码语言:javascript
复制
from collections import deque
queue = deque(["Eric", "John", "Michael"])

如何检查此队列的长度?

我们可以像这样初始化吗?

代码语言:javascript
复制
queue = deque([])   #is this length 0 deque?
EN

回答 3

Stack Overflow用户

发布于 2016-09-27 14:24:12

只需使用.qsize()示例即可:

代码语言:javascript
复制
a=Queue()
a.put("abcdef")
print a.qsize() #prints 1 which is the size of queue

上面的代码片段适用于python的Queue()类。感谢@rayryeng的更新。

对于deque from collections,我们可以使用len(),正如K Z声明的here

票数 56
EN

Stack Overflow用户

发布于 2019-02-22 20:37:32

是的,我们可以检查从集合创建的队列对象的长度。

代码语言:javascript
复制
from collections import deque
class Queue():
    def __init__(self,batchSize=32):
        #self.batchSie = batchSize
        self._queue = deque(maxlen=batchSize)

    def enqueue(self, items):
        ''' Appending the items to the queue'''
        self._queue.append(items)

    def dequeue(self):
        '''remoe the items from the top if the queue becomes full '''
        return self._queue.popleft()

创建类的对象

代码语言:javascript
复制
q = Queue(batchSize=64)
q.enqueue([1,2])
q.enqueue([2,3])
q.enqueue([1,4])
q.enqueue([1,22])

现在检索队列的长度

代码语言:javascript
复制
#check the len of queue
print(len(q._queue)) 
#you can print the content of the queue
print(q._queue)
#Can check the content of the queue
print(q.dequeue())
#Check the length of retrieved item 
print(len(q.dequeue()))

检查附加屏幕截图中的结果

希望这能帮到你。

票数 1
EN

Stack Overflow用户

发布于 2015-12-06 17:28:08

使用queue.rear+1获取队列的长度

票数 -7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12548481

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档