当对循环进行扁平化处理时,您将执行如下操作:
for a, b, c in itertools.product(x1, x2, x3):
...但是,如何处理其中一个元素被用作参数来检索也要遍历的列表的情况呢?例如:
for a, b in itertools.product(x1, get_b_elements(a)):
...这有可能吗?
发布于 2016-12-06 02:59:19
for a in x1:
for b in get_b_elements(a):
#do something with (a, b)我只想列举一个替代方案:
for (a, b) in [(a_i, b_i) for a_i in x1 for b_i in get_b_elements(a_i)]:
#do something with (a, b)正如@wim注意到的,这些“扁平”循环都不是你想要的。
发布于 2016-12-06 03:25:58
试试这个:
alphabets = [a,b,c,d]
xs = [x1,x2,x3]
itertools.product(alphabets, xs)https://stackoverflow.com/questions/40987074
复制相似问题