python deque

作用

from collections import deque

deque和c++中stl的deque相似,是一种双向队列,底层据说也是同样用双链表实现的

可以用于多线程的线程池的实现,或者消息队列的实现

函数

最常用的,记住append popleft pop 和len就可以了

append, appendleft, pop, popleft

index:和list的相类似,用于找出某个值第一个匹配项的索引位置。

remove:同list:用于移除列表中某个值的第一个匹配项。

insert:和list的相同,插入元素insert(index,obj),在index前插入元素,如果index超过长度就会插到最后,如果长度已经是最长,再插入会报错

maxlen:用于定义的时候使用,不是一个可以被writed的对象,形如a.maxlen=10会报错

reverse:倒序

rotate:循环移动,为正全体右移,为负全体左移

leetcode 933

class RecentCounter(object):

    def __init__(self):
        self.pings = collections.deque()

    def ping(self, t):
        """
        :type t: int
        :rtype: int
        """
        self.pings.append(t)
        while(self.pings[0] < t-3000):
            self.pings.popleft()
        return len(self.pings)
原文地址:https://www.cnblogs.com/wangjiale1024/p/11437918.html