2

Let's say I have a 2-d numpy array with 10 rows

for example

array([[  23425.     ,  521331.40625],
   [  23465.     ,  521246.03125],
   [  23505.     ,  528602.8125 ],
   [  23545.     ,  531934.75   ],
   [  23585.     ,  534916.375  ],
   [  23625.     ,  544971.9375 ],
   [  23665.     ,  544707.5625 ],
   [  23705.     ,  532729.25   ],
   [  23745.     ,  540303.0625 ],
   [  23865.     ,  527971.1875 ]])

Is there a way to place that whole array in a queue (from python's collections) all at once, without iterating over the array and using put() for each row, and then be able to retrieve each row separately using the queue.get() function?

For example a first call to the queue.get() would retrieve [23865., 527971.1875 ] and a second call would retrieve [23745., 540303.0625 ]

LetsPlayYahtzee
  • 7,161
  • 12
  • 41
  • 65
  • do you want each item put in the queue or each row in your array? – Zachi Shtain Feb 18 '16 at 14:20
  • The documentation page shows that only the `put()` method allows inserts, so I would say no. It also mentions a thread-safe and lock-less `deque` type on the same page which does allow you to provide an entire iterable to the constructor. – Kurt Stutsman Feb 18 '16 at 14:21

1 Answers1

6

You can use the map keyword to avoid iterating over the array:

map(queue.put, myArray)

or in python 3.x:

list(map(queue.put, myArray))

Zachi Shtain
  • 826
  • 1
  • 13
  • 31
  • that's what I was looking for, should I delete the question as [duplicate](http://stackoverflow.com/questions/31987207/put-multiple-items-in-a-python-queue)? – LetsPlayYahtzee Feb 18 '16 at 14:23
  • I'm not sure your question is a duplicate to an existing one. I wouldn't delete it unless someone would tell you it is a duplicate – Zachi Shtain Feb 18 '16 at 14:25
  • This doesn't work in Python 3 since it does lazy evaluation of maps. You would need to do some ugly thing like `list(map(queue.put, myArray))` but at that point I guess a loop is justifyable – Stefan Apr 26 '17 at 11:56