在python 2.6中检查双端队列的maxlen

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

我不得不从 python 2.7 更改为 2.6.我一直在使用带有 maxlen 属性的双端队列,并且一直在检查 maxlen 是什么.显然你可以在 python 2.6 中使用 maxlen,但在 2.6 双端队列中没有 maxlen 属性.在 python 2.6 中检查双端队列的 maxlen 的最简洁方法是什么?

在 2.7 中:

from collections import dequed = 双端队列(最大长度 = 10)打印 d.maxlen

在 2.6 中可以使用 deque,并且 maxlen 可以正常工作,但是 maxlen 不是可以引用的属性.

干杯

解决方案

我将通过继承 collections.deque 来创建我自己的 deque.这并不困难.也就是说,这里是:

导入集合类双端队列(collections.deque):def __init__(self, iterable=(), maxlen=None):super(deque, self).__init__(iterable, maxlen)self._maxlen = maxlen@财产def maxlen(自我):返回 self._maxlen

这是工作中的新双端队列:

>>>d = 双端队列()>>>打印双端队列([])>>>打印 d.maxlen没有任何>>>d = 双端队列(最大长度 = 3)>>>打印双端队列([], maxlen=3)>>>打印 d.maxlen3>>>d = 双端队列(范围(5))>>>打印双端队列([0, 1, 2, 3, 4])>>>打印 d.maxlen没有任何>>>d = 双端队列(范围(5),maxlen=3)>>>打印双端队列([2, 3, 4], maxlen=3)>>>打印 d.maxlen3

I have had to change from python 2.7 to 2.6. I've been using a deque with the maxlen property and have been checking what the maxlen is. Apparently you can use maxlen in python 2.6, but in 2.6 deques do not have a maxlen attribute. What is the cleanest way to check what the maxlen of a deque is in python 2.6?

In 2.7:

from collections import deque d = deque(maxlen = 10) print d.maxlen

In 2.6 the deque can be used and the maxlen works properly, but maxlen is not an attribute that can be referred to.

Cheers

解决方案

I would create my own deque by inheriting from collections.deque. It is not difficult. Namely, here it is:

import collections class deque(collections.deque): def __init__(self, iterable=(), maxlen=None): super(deque, self).__init__(iterable, maxlen) self._maxlen = maxlen @property def maxlen(self): return self._maxlen

and this is the new deque at work:

>>> d = deque() >>> print d deque([]) >>> print d.maxlen None >>> d = deque(maxlen=3) >>> print d deque([], maxlen=3) >>> print d.maxlen 3 >>> d = deque(range(5)) >>> print d deque([0, 1, 2, 3, 4]) >>> print d.maxlen None >>> d = deque(range(5), maxlen=3) >>> print d deque([2, 3, 4], maxlen=3) >>> print d.maxlen 3

更多推荐

在python 2.6中检查双端队列的maxlen

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

发布评论

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

>www.elefans.com

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