2

I'm working on a list that receives new elements once in a while. When these new elements have been added, I want to perform a computation over these elements (to be precise, estimate a KDE). I quickly realized that if this list were to grow unbounded, the computation of the KDE function would take extremely long, so I thought a Queue would be a good data structure to use. The standard Python Queue (https://docs.python.org/2/library/queue.html), however, does not allow for access to individual Queue elements without 'popping' them out of the queue. Is there any alternative?

In other words: is there some Python library that allows me to get a queue element without popping it? (or that allows array-like indexing of the queue elements?)

11
  • 2
    Use the deque module Commented Jul 2, 2014 at 12:41
  • 1
    I don't know what a KDE is, but a queue is something where you add things to the end and pop them at the beginning; with arbitrary access it's not a queue. Is there any reason you can't just use a list? Commented Jul 2, 2014 at 12:43
  • You should think about the other data structure, e.g. list. Queue is not what you are looking for. Commented Jul 2, 2014 at 12:44
  • @RemcoGerlich well, I could use a list, but I want it to be capped at a maximum number of elements. When the list is full and new elements arrive, I'd like the new elements to be added, and the oldest elements to be removed. Therefore, a queue seemed like the most natural solution Commented Jul 2, 2014 at 12:45
  • @NiklasB. Thanks! Looks good so far :), this should probably work Commented Jul 2, 2014 at 12:46

3 Answers 3

5

It sounds like you would get good use from using deque:

https://docs.python.org/2/library/collections.html#collections.deque

Sign up to request clarification or add additional context in comments.

2 Comments

@Niklas B (above) suggested the same. Looks good so far, thanks!
But note that it's optimized for access at the ends, so it's probably slower than a list when using indexing to get at data somewhere in the middle.
0

I dont understand why u use queue if u dont use the popping mechanism. If u are wondering about to owercrowd in your array u may use 1 array and 1 queue. First is waiting queue and second is processing array.

And u may do some optimizations about your loop to speed it up.

For example u may change

import xxx

for a in b_array:
   xxx.do_something(a)

to this:

import xxx

ds = xxx.do_something  #linking a function in memory speeds up foreach performance very much
for a in b_array:
   ds(a)

I think your problem is not about queue size. If it is, u must check your early code.

1 Comment

The queue size will be problematic for the dataset I am dealing with. This dataset generates 1000 points per second, so storing them in memory is not feasible, let alone suitable for computation (sorry, didn't mention the exact use case)
0

As @RemcoGerlich suggested, the best way forward (I believe) is to maintain an index pointer that 'memorizes' where the next suitable write position is, modulo the size of the list. This will allow for very fast implementation using numpy and will also allow me to achieve the goals I specified.

2 Comments

Won't your list get too long for memory if there are 1000 points per second? Note that the time needed for list operations (adding an item to the end, checking its length and popping an item from the beginning if it's too long) may be negligible compared to the time needed for the KDE computation.
Yes, I intend to cap the size of the list at some fixed number to optimize KDE computation time and ensure that it fits in memory

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.