deque对象

https://docs.python.org/2/library/collections.html#collections.deque

class collections.deque([ iterable [,maxlen ] ] )

返回一个从左到右(使用append())初始化的新deque对象,其中包含来自iterable的数据。如果未指定iterable,则新的deque为空。

Deques是堆栈和队列的概括(名称发音为“deck”,是“双端队列”的缩写)。Deques支持线程安全,内存有效的附加和从双端队列的弹出,在任一方向上具有大致相同的O(1)性能。

尽管list对象支持类似的操作,但它们针对快速固定长度操作进行了优化,并且导致O(n)内存移动成本pop(0)和操作,这些成本 改变了底层数据表示的大小和位置。insert(0, v)

版本2.4中的新功能。

如果未指定maxlenNone,则deques可能会增长到任意长度。否则,双端队列限制为指定的最大长度。一旦有界长度双端队列已满,当添加新项目时,从对方端丢弃相应数量的项目。有界长度deques提供类似于tailUnix中的过滤器的功能。它们还可用于跟踪仅涉及最近活动的事务和其他数据池。

在2.6版中更改:添加了maxlen参数。

Deque对象支持以下方法:

append

x添加到双端队列的右侧。

appendleft

x添加到双端队列的左侧。

clear()

删除deque中的所有元素,使其长度为0。

count

计算deque元素的数量等于x

版本2.7中的新功能。

extend可迭代的

通过附加可迭代参数中的元素来扩展双端队列的右侧。

extendleft可迭代的

通过附加来自iterable的元素来扩展双端队列的左侧。注意,左边追加的系列会导致反转迭代参数中元素的顺序。

pop()

从双端队列的右侧移除并返回一个元素。如果没有元素,则提出一个IndexError

popleft()

从双端队列的左侧移除并返回一个元素。如果没有元素,则提出一个IndexError

remove

删除第一次出现的。如果没有找到,提出一个 ValueError

版本2.5中的新功能。

reverse()

在原位反转deque的元素然后返回None

版本2.7中的新功能。

rotaten = 1 

向右旋转deque n步。如果n为负数,则向左旋转。

当双端队列不为空时,向右 d.appendleft(d.pop())旋转一步相当于,向左旋转一步相当于d.append(d.popleft())

Deque对象还提供一个只读属性:

maxlen

一个双端队列的最大大小或None无边界。

版本2.7中的新功能。

除上述外,双端支持迭代,酸洗,len(d), reversed(d)copy.copy(d)copy.deepcopy(d),会员与测试in操作,并标引用,如d[-1]。索引访问在两端都是O(1),但在中间减慢到O(n)。对于快速随机访问,请改用列表。

例:

>>> from collections import deque
>>> d = deque('ghi')                 # make a new deque with three items
>>> for elem in d:                   # iterate over the deque's elements
...     print elem.upper()
G
H
I

>>> d.append('j')                    # add a new entry to the right side
>>> d.appendleft('f')                # add a new entry to the left side
>>> d                                # show the representation of the deque
deque(['f', 'g', 'h', 'i', 'j'])

>>> d.pop()                          # return and remove the rightmost item
'j'
>>> d.popleft()                      # return and remove the leftmost item
'f'
>>> list(d)                          # list the contents of the deque
['g', 'h', 'i']
>>> d[0]                             # peek at leftmost item
'g'
>>> d[-1]                            # peek at rightmost item
'i'

>>> list(reversed(d))                # list the contents of a deque in reverse
['i', 'h', 'g']
>>> 'h' in d                         # search the deque
True
>>> d.extend('jkl')                  # add multiple elements at once
>>> d
deque(['g', 'h', 'i', 'j', 'k', 'l'])
>>> d.rotate(1)                      # right rotation
>>> d
deque(['l', 'g', 'h', 'i', 'j', 'k'])
>>> d.rotate(-1)                     # left rotation
>>> d
deque(['g', 'h', 'i', 'j', 'k', 'l'])

>>> deque(reversed(d))               # make a new deque in reverse order
deque(['l', 'k', 'j', 'i', 'h', 'g'])
>>> d.clear()                        # empty the deque
>>> d.pop()                          # cannot pop from an empty deque
Traceback (most recent call last):
  File "<pyshell#6>", line 1, in -toplevel-
    d.pop()
IndexError: pop from an empty deque

>>> d.extendleft('abc')              # extendleft() reverses the input order
>>> d
deque(['c', 'b', 'a'])

猜你喜欢

转载自blog.csdn.net/qq_29678299/article/details/90036647
今日推荐