gpt4 book ai didi

python:itertools.product 上的队列问题

转载 作者:行者123 更新时间:2023-11-30 22:58:52 25 4
gpt4 key购买 nike

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() 的结果(返回“Chrysler”)提供给 itertools.product,然后迭代它:” C”、“h”、“r”...

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

关于python:itertools.product 上的队列问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35999547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com