Python Datatype for a fixed-length FIFO

前端 未结 3 1924
孤城傲影
孤城傲影 2020-12-13 08:14

I would like to know if there is a native datatype in Python that acts like a fixed-length FIFO buffer. For example, I want do create a length-5 FIFO buffer that is initial

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-13 08:49

    x = collections.deque(5*[0], 5)
    

    See the docs for more about collections.deque; the method you call push is actually called appendleft in that type.

    The second parameter (maxlen, giving the maximum lengths) was added in Python 2.6; if you're using older versions of Python, it won't be available.

提交回复
热议问题