是否可以在python中将列表转换为队列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21639888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to convert list to queue in python?
提问by rggod
How to convert a list to queue? So that operations like enqueue or dequeue an be carried out. I want to use to the list to remove the top most values and i believe it can be done using queues.
如何将列表转换为队列?这样就可以执行入队或出队等操作。我想使用列表来删除最高值,我相信可以使用队列来完成。
回答by msvalkon
You can use a list as a queue. If you want a fifo queue, just use .append()to add and .pop(0)to remove. For a lifo queue (i.e a stack), use .append()to add and .pop()to remove.
您可以将列表用作队列。如果你想要一个先进先出队列,只需使用.append()添加和.pop(0)删除。对于 lifo 队列(即堆栈),使用.append()添加和.pop()删除。
You should use collections.dequewhen implementing a fifo-queue which was designed specifically for this purpose. .pop(0)is a O(n) operation. Using a list as a stack is just fine.
在实现专为此目的设计的 fifo-queue 时,您应该使用collections.deque。.pop(0)是一个 O(n) 操作。使用列表作为堆栈就好了。
FIFO Queue:
先进先出队列:
In [1]: q = range(15)
In [2]: q.pop(0)
Out[2]: 0
In [3]: q.pop(0)
Out[3]: 1
In [4]: q.pop(0)
Out[4]: 2
LIFO Queue:
后进先出队列:
In [5]: q = range(10)
In [6]: q.pop()
Out[6]: 9
In [7]: q.pop()
Out[7]: 8
In [8]: q.pop()
Out[8]: 7
回答by gefei
just use pop()
只是使用 pop()
>>> x = [1,2,3]
>>> x.pop(0)
1
>>> x
[2,3]
回答by John La Rooy
回答by dstromberg
collections.deque is the standard answer, though it's not abstracted terribly well.
collections.deque 是标准答案,尽管它没有很好地抽象。
There's also https://pypi.python.org/pypi/linked_list_mod/, if you're willing to sacrifice a little speed for better abstraction. collections.deque is faster. linked_list_mod lets you pass an iterable to the constructor; the provided lifo and fifo modules do not, but could trivially be modified to do so.
还有https://pypi.python.org/pypi/linked_list_mod/,如果您愿意牺牲一点速度以获得更好的抽象。collections.deque 更快。Linked_list_mod 允许您将一个可迭代对象传递给构造函数;提供的 lifo 和 fifo 模块没有,但可以对其进行简单的修改。
回答by maxb
Since I was looking for an answer to this question while using queue.Queue, I thought I should share my findings. It is possible to convert a list into a queue using queue.queue.
由于我在使用 时正在寻找这个问题的答案queue.Queue,我想我应该分享我的发现。可以使用 将列表转换为队列queue.queue。
import queue
l = [i for i in range(1000)]
q = queue.Queue()
[q.put(i) for i in l]
q2 = queue.Queue()
q2.queue = queue.deque(l)
After this code has been run, qand q2are two different queues that contain the exact same entries, but with the second method being >300 times faster on my machine.
此代码后一直运行,q并且q2是包含完全相同的条目两个不同的队列,但与第二种方法是>快300倍我的机器上。
Not related to the question, but the opposite can be done by l = list(q.queue)if qis an instance of queue.Queue. Hope this saves you some trouble!
与问题无关,但相反的可以通过l = list(q.queue)ifq是 的实例来完成queue.Queue。希望这可以为您节省一些麻烦!
This was all tested in python 3.5.2.
这一切都在 python 3.5.2 中进行了测试。

