python:itertools.product 上的队列问题

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?

因为您将 cars_q.get()(其中 returns "Chrysler")的结果提供给 itertools.product,然后对其进行迭代:"C", "h", "r"...

你知道Queue是什么吗?您是否在寻找 collections.deque?

尝试将汽车元组放入 cars_q。

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