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

编程入门 行业动态 更新时间:2024-10-09 01:24:02
本文介绍了如何在 Python 中检查双端队列长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

How to check a deque's length in python?

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

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

docs.python/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 中检查双端队列长度

本文发布于:2023-11-30 06:48:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649072.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:队列   长度   如何在   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!