I am writing a script that uses a queue to track the last six numbers of a list of length N. In other words, as I iterate over the list of length N, I want to track the last six numbers. I thought a queue would be a good structure to do this because it would automatically push out the last 6th number as I push in a new number. My code is below:
if __name__ == '__main__':
data = [1,23,45,5,43,2,54,2,34,32]
scub = DataScrubber(data)
scub.scrub_zeros(0.01)
print "TEST"
def scrub_zeros(self,upperzero_range):
scrubbed_data = []
last_6_data_points = Queue.Queue(6)
for data in self.data:
print last_6_data_points
print scrubbed_data
last_6_data_points.put(data)
scrubbed_data.append(data)
self.data = scrubbed_data
When I run this script I get the following output:
<Queue.Queue instance at 0x03E25D00>
[]
<Queue.Queue instance at 0x03E25D00>
[1]
<Queue.Queue instance at 0x03E25D00>
[1, 23]
<Queue.Queue instance at 0x03E25D00>
[1, 23, 45]
<Queue.Queue instance at 0x03E25D00>
[1, 23, 45, 5]
<Queue.Queue instance at 0x03E25D00>
[1, 23, 45, 5, 43]
<Queue.Queue instance at 0x03E25D00>
[1, 23, 45, 5, 43, 2]
However, this script never stops running. It freezes and I have to break out using ctrl+break. So after thinking, I figured out the reason why it is freezing. It freezes because when I "put" data into the queue, is doesn't automatically push ot the older data at the other end of the queue. So I guess that the queue is stuck. Am I missing something, is that not suppose to be the point of how a queue works? Do I have to call "get"? Is there anyway around having to call another function for the queue, where it will automatically push out the oldest data when new data is pushed into the queue?
My second question:
How can I view the entire queue like a list? So:
print my_queue
Results in:
[null,1, 23, 45, 5, 43]
after pushing in 5 numbers into 6 line queue.