IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    [原]5.3.3 deque对象

    caimouse发表于 2015-12-27 09:50:10
    love 0

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

    返回一个新双向队列,当有输入迭代器时,会从左至右地添加到队列里。如果没有输入参数,就创建一个空队列。

    deque是一个具有栈和队列特性的数据结构。它支持线程安全、内存优化和两端弹出、插入元素,无论从那一个方向弹出元素都是O(1)的时间花费。在内置的数据类型list也支持相关的操作,但是它设计为对固定元素进行操作,如果插入和弹出一个元素,它的内存操作时间花费是o(n)。如果参数maxlen没有指定,或者指定为None,它的长度是任意的,如果有指定长度,就不能添加元素超过指定长度。如果队列已经达到指定长度,从一端添加一个元素,就会从另一端弹出一个元素,从而保持元素不变。因而它非常适合跟踪最后活动对象的场合。

    例子:

    #python 3.4

    import collections

     

    dq = collections.deque(maxlen = 5)

    dq.append(1)

    print(dq)

    结果输出如下:

    deque([1], maxlen=5)

     

    deque主要支持以下方法:

    append(x)

    添加元素x到队列的右边。

    例子:

    #python 3.4

    import collections

     

    dq = collections.deque(maxlen = 5)

    dq.append(1)

    dq.append(2)

    dq.append(3)

    print(dq)

    结果输出如下:

    deque([1, 2, 3], maxlen=5)

     

    appendleft(x) 

    添加元素x到队列左边。

    例子:

    #python 3.4

    import collections

     

    dq = collections.deque(maxlen = 5)

    dq.appendleft(1)

    dq.appendleft(2)

    dq.appendleft(3)

    print(dq)

    结果输出如下:

    deque([3, 2, 1], maxlen=5)

     

    clear() 

    清除队列里所有元素。

    例子:

    #python 3.4

    import collections

     

    dq = collections.deque(maxlen = 5)

    dq.appendleft(1)

    dq.appendleft(2)

    dq.appendleft(3)

    print(dq)

    dq.clear()

    print(dq)

    结果输出如下:

    deque([3, 2, 1], maxlen=5)

    deque([], maxlen=5)

     

    count(x) 

    计算队列元素的个数是否等于x个,如果大于或等于都返回True,否则返回False。

    例子:

    #python 3.4

    import collections

     

    dq = collections.deque(maxlen = 5)

    dq.append(1)

    dq.append(2)

    dq.append(3)

    print(dq)

    print(dq.count(1))

    print(dq.count(2))

    print(dq.count(3))

    print(dq.count(6))

    结果输出如下:

    deque([1, 2, 3], maxlen=5)

    1

    1

    1

    0

     

    extend(iterable) 

    从右边扩展队列,参数是迭代器对象。

    例子:

    #python 3.4

    import collections

     

    dq = collections.deque(maxlen = 5)

    dq.append(1)

    dq.append(2)

    dq.append(3)

    print(dq)

    dq.extend([7, 8, 9])

    print(dq)

    结果输出如下:

    deque([1, 2, 3], maxlen=5)

    deque([2, 3, 7, 8, 9], maxlen=5)

     

    extendleft(iterable) 

    从左边添加迭代对象里的元素。注意添加顺序与迭代对象顺序刚好相反。

    例子:

    #python 3.4

    import collections

     

    dq = collections.deque(maxlen = 5)

    dq.append(1)

    dq.append(2)

    dq.append(3)

    print(dq)

    dq.extendleft([7, 8, 9])

    print(dq)

    结果输出如下:

    deque([1, 2, 3], maxlen=5)

    deque([9, 8, 7, 1, 2], maxlen=5)

     

    pop() 

    从右边删除一个元素,并且把这个元素返回。如果队列为空,抛出异常IndexError。

    例子:

    #python 3.4

    import collections

     

    dq = collections.deque(maxlen = 5)

    dq.append(1)

    dq.append(2)

    dq.append(3)

    print(dq)

    dq.pop()

    print(dq)

    输出结果如下:

    deque([1, 2, 3], maxlen=5)

    deque([1, 2], maxlen=5)

     

    popleft() 

    从左边删除一个元素,并把这个元素返回。如果是空队列会抛出异常IndexError。

    例子:

    #python 3.4

    import collections

     

    dq = collections.deque(maxlen = 5)

    dq.append(1)

    dq.append(2)

    dq.append(3)

    print(dq)

    dq.popleft()

    print(dq)

    结果输出如下:

    deque([1, 2, 3], maxlen=5)

    deque([2, 3], maxlen=5)

     

    remove(value) 

    删除指定值value的元素,如果没有找到,会抛出异常IndexError。

    例子:

    #python 3.4

    import collections

     

    dq = collections.deque(maxlen = 5)

    dq.append(9)

    dq.append(2)

    dq.append(3)

    dq.append(8)

    print(dq)

    dq.remove(9)

    print(dq)

    结果输出如下:

    deque([9, 2, 3, 8], maxlen=5)

    deque([2, 3, 8], maxlen=5)

     

    reverse() 

    把所有元素进行反向排序,返回None。

    例子:

    #python 3.4

    import collections

     

    dq = collections.deque(maxlen = 5)

    dq.append(9)

    dq.append(2)

    dq.append(3)

    dq.append(8)

    print(dq)

    dq.reverse()

    print(dq)

    结果输出如下:

    deque([9, 2, 3, 8], maxlen=5)

    deque([8, 3, 2, 9], maxlen=5)

     

    rotate(n) 

    如果n是正值,则从右边弹出元素,插入到左边,n是代表操作几次。如果n是负值,刚好相反,表示从左边弹出元素,插入到右边。大体上等于d.appendlef(d.pop())。

    例子:

    #python 3.4

    import collections

     

    dq = collections.deque(maxlen = 5)

    dq.append(9)

    dq.append(2)

    dq.append(3)

    dq.append(8)

    print(dq)

    dq.rotate(2)

    print(dq)

    dq.rotate(-2)

    print(dq)

    结果输出如下:

    deque([9, 2, 3, 8], maxlen=5)

    deque([3, 8, 9, 2], maxlen=5)

    deque([9, 2, 3, 8], maxlen=5)

     

    deque类提供一个只读的属性:

    maxlen 

    返回列表允许的最大长度,如果没有设置返回None。

    例子:

    #python 3.4

    import collections

     

    dq = collections.deque(maxlen = 5)

    dq.append(9)

    dq.append(2)

    dq.append(3)

    dq.append(8)

    print(dq.maxlen)

    结果输出如下:

    5

     

    deque类支持迭代、选取、len(d)、reversed(d)、copy.copy(d)、copy.deepcopy(d)等操作,同时也支持in操作符,以使用索引d[-1]的操作。



    蔡军生 QQ:9073204  深圳



沪ICP备19023445号-2号
友情链接