Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dumping queue into list/array in python

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?

 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.

like image 792
thetna Avatar asked Jul 18 '13 08:07

thetna


People also ask

Can we use list as a queue in Python?

Implementation using list. List is a Python's built-in data structure that can be used as a queue. Instead of enqueue() and dequeue(), append() and pop() function is used.

How do you deque a list in Python?

The pop() method is used to remove and return the right most element from the queue, and popleft() method is used to remove and return left most element from the queue.

How do I turn a list into a queue?

Use the collections. deque class to convert a list to a queue in Python, e.g. deq = deque(my_list) . The deque class can be passed an iterable, such as a list, and initializes a new deque object.

Can you index a queue in Python?

You can not. Because it is not a sequence. If you want to index it, probably make a list . For a queue q , list(q) won't work as q is not iterable.


1 Answers

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]
like image 166
grovina Avatar answered Sep 19 '22 13:09

grovina