Search code examples
pythonqueuepython-itertools

python: queue issue on itertools.product


import itertools
import Queue

cars = ["Chrysler", "Ford", "LeSabre", "Jeep", "pontiac" ]
colors = ["white", "green", "blue", "silver", "red"]

cars_q = Queue.Queue()
for car in cars:
    cars_q.put(car)
    print cars_q.qsize(), car

while not cars_q.empty():
    for comb in enumerate(itertools.product(cars_q.get(), colors)):
       print comb

the script should do all combinations of cars-colors and with two lists works fine but if I put a list on queue the output is:

(0, ('C', 'white'))
(1, ('C', 'green'))
(2, ('C', 'blue'))
(3, ('C', 'silver'))
(4, ('C', 'red'))
(5, ('h', 'white'))

why queue take only the first char?


Solution

  • Try this to put a tuple of car into cars_q.

    for car in cars:
        cars_q.put(tuple([car]))
        print cars_q.qsize(), car