Search code examples
python-3.xqueuepriority-queue

Redirecting queues to priority queue


Hi there I'm new to Python so forgive me if I'm asking for too much. I'm using the queue module to create these. What I'm trying to do is place a string into the one queue and then redirect it to two others based on the substring.

For example my code is:

import queue

firstQueue = queue.Queue()
secondQueue = queue.PriorityQueue()
thirdQueue = queue.PriorityQueue()

var1 = "Hello World!"
var2 = "Hello Stack!"
var3 = "Hello Wally!"

firstQueue.put(var1)
firstQueue.put(var2)
firstQueue.put(var3)

At this point I want to redirect the the firstQueue to the secondQueue if the 7th character starts with 'W' in this case 'World/Wally' and anything else into the third (assuming all strings put into the first is the same length). Any help and suggestions is much appreciated!


Solution

  • use loop to go through each element in first Queue and check the condition and put it in second Queue,like this:

    while not firstQueue.empty():
        element = firstQueue.get()
        if(element[6] == 'W'):
            secondQueue.put(element)
    
    while not secondQueue.empty():
        print(secondQueue.get())