如何在 Python 中检查双端队列长度 [英] How to check deque length in Python

查看:67
本文介绍了如何在 Python 中检查双端队列长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在python中检查双端队列的长度?

How to check a deque's length in python?

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

I don't see they provide deque.length in Python...

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

from collections import deque
queue = deque(["Eric", "John", "Michael"])

如何查看这个双端队列的长度?

How to check the length of this deque?

我们可以像这样初始化

queue = deque([])   #is this length 0 deque?

推荐答案

len(queue) 应该给你结果,在这种情况下是 3.

len(queue) should give you the result, 3 in this case.

具体来说,len(object) 函数会调用 object.__len__ 方法 [参考链接].而本例中的对象是deque,它实现了__len__方法(可以通过dir(deque)看到).

Specifically, len(object) function will call object.__len__ method [reference link]. And the object in this case is deque, which implements __len__ method (you can see it by dir(deque)).

queue= deque([])   #is this length 0 queue?

是的,对于空的 deque,它将是 0.

Yes it will be 0 for empty deque.

这篇关于如何在 Python 中检查双端队列长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆