I'm fairly certain you want a deque
from the collections
module. It includes (among other things) append, pop, pop_left, and rotate methods, and also supports indexing. Indexing slows toward the middle, but is fast at the ends.
Python Queue-Like Container
-
05-07-2023 - |
문제
I would like a container to do the following in python 2-7:
I need that container to behave like a queue: first in first out, I append to it objects and then get them in the same order "from the other end".
However I also need to be able to read up to 5 objects from the beginning of the queue without popping them, then if I don't need them anymore I will pop them from the queue.
I am new to python and I need to know is there any container that would act as such? Or any easy simple implementation for it?
해결책
다른 팁
See this module from the standard library. It should provide you with everything you want.
http://my.safaribooksonline.com/book/programming/python/9780132778633/data-structures/ch02lev1sec5
import Queue
q = Queue.Queue()
for i in range(5):
q.put(i)
while not q.empty():
print q.get(),
print
제휴하지 않습니다 StackOverflow