将队列转储到python中的列表/数组中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17718265/
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
dumping queue into list/array in python
提问by thetna
I am running a number of threads and collecting there result on a queue. I would like to dump it into array or list so that I can do indexing and retrieve those results. Each of the elements in the queue is a array of dimension n. I would like to access those arrays. Would you please let me know, how would i do it?
我正在运行多个线程并在队列中收集结果。我想将它转储到数组或列表中,以便我可以进行索引并检索这些结果。队列中的每个元素都是一个维度为 n 的数组。我想访问这些数组。请你告诉我,我该怎么做?
def dump_queue(model_queue):
queue_list = []
for i in iter(model_queue.get,'STOP'):
queue_list.append(i)
return queue_list
aux_model=train_svm(np.array(trainExample),np.array(trainLabel))
model_queue.put(aux_model.coef_)
Thus the arrays are the learned model parameters of svm
. model_queue is shared among the threads. I want to access each of the model parameters vectors not each of the entries of a model parameters.
因此,数组是 的学习模型参数svm
。model_queue 在线程之间共享。我想访问每个模型参数向量而不是模型参数的每个条目。
回答by Skineffect
Like Gabriel said, you should specifiy your questions.
就像加布里埃尔所说,你应该具体说明你的问题。
When you speak of arrays, I think you refer to lists. Python Arrays are used to store numerical values.
当您谈到数组时,我认为您指的是列表。Python 数组用于存储数值。
However, what you could do to transform your queue to an nested list:# output list:
但是,您可以将队列转换为嵌套列表:# 输出列表:
collectedData = list()
# # #
# for every thread, call:
threadBuffer = [item for item in q.queue]
collectedData.append(threadBuffer)
This will collect your data in nested lists, which you can easily access, like in this example:
这将在嵌套列表中收集您的数据,您可以轻松访问这些列表,如下例所示:
l = list()
l.append([1,2,3,4,5,6,7,9,10])
l.append([6,7,8,9,0,11,22,33])
Now you can access freely: l[1][2] -> 8
现在您可以自由访问:l[1][2] -> 8
Does this help you?
这对你有帮助吗?
Best regards, Christian
最好的问候,基督徒
回答by robrant
Not sure whether it's the same problem, but I needed to do the same thing and ended up writing this. I am using threading.Thread and the Queue object in python 2.7. and just wanted to dump out a queue to a list.
不确定这是否是同样的问题,但我需要做同样的事情并最终写了这个。我在 python 2.7 中使用 threading.Thread 和 Queue 对象。并且只想将队列转储到列表中。
def queue_to_list(q):
""" Dump a Queue to a list """
# A new list
l = []
while q.qsize() > 0:
l.append(q.get())
return l
回答by grovina
You're done with the parallel part and just want to get the results in a list, is that it? Then try:
你已经完成了并行部分,只是想在一个列表中得到结果,是吗?然后尝试:
list(my_queue.queue)
Example:
例子:
from queue import Queue
q = Queue()
for i in range(5):
q.put(i)
l = list(q.queue)
print(l)
Output:
输出:
[0, 1, 2, 3, 4]
回答by Jannick
I would go with the following solution:
我会采用以下解决方案:
from collections import deque
q = deque()
for i in range(5):
q.append([i,i+10])
listed_q = list(q)
Now you can easily access the values by indexing or slicing:
现在您可以通过索引或切片轻松访问这些值:
print listed_q[1] # [1, 11]
print listed_q[1][1] # 11
Since list() creates a copy of q, you should keep an eye on the dimensionality of your queue. The time needed to do this operation grows the longer your queue is. An alternative approach can be found by using itertools.islice where you first slice the queue before you store the outcome in a list. Check out the following links (performance measures are also given there):
由于 list() 创建了 q 的副本,因此您应该注意队列的维度。您的队列越长,执行此操作所需的时间就越长。可以使用 itertools.islice 找到另一种方法,在将结果存储在列表中之前,首先对队列进行切片。查看以下链接(此处还提供了性能度量):
Use slice notation with collections.deque