Python 带有固定参数的 itertools
Python itertools with fix parameters
我正在尝试做一个可迭代但有一些固定参数,并且可迭代参数在列表中。
这是我正在寻找的输出:
(fix1, fix2, fix3, [iterable1_1, iterable2_1, iterable3_1], fix4)
(fix1, fix2, fix3, [iterable1_1, iterable2_1, iterable3_2], fix4)
等基本上,只有列表中的三个发生变化;其余不变。
到目前为止,我已经试过了,但它并没有真正奏效。
iterable = itertools.product([fix1], [fix2], [fix3], [[iter1_1, iter1_2, iter1_3], [iter2_1, iter2_2], [iter3_1, iter3_2, iter3_3]], [fix4])
iter1、iter2 和 iter3 的长度不同,但我认为这不相关。
一个例子:
我有两个列表,a = [1,2] 和 b = [3,4] 以及一些固定参数 f1 = 10、f2=20、f3=30
所需的输出将是:
(10, 20, [1,3], 30)
(10, 20, [1,4], 30)
(10, 20, [2,3], 30)
(10, 20, [2,4], 30)
如有疑问,请创建函数。
def framed(*iters):
for pair in itertools.product(*iters):
yield (10, 20, pair, 30)
for result in framed([1, 2], [3, 4]):
print result
(10, 20, (1, 3), 30)
(10, 20, (1, 4), 30)
(10, 20, (2, 3), 30)
(10, 20, (2, 4), 30)
听起来你想要这样的东西:
result = [(fix1, fix2, fix3, [a, b, c], fix4)
for a, b, c in itertools.product(iterable1, iterable2, iterable3)]
如果带有a, b, c
的内部序列可以是元组而不是列表,则不需要解包:
result = [(fix1, fix2, fix3, prod, fix4) for prod in product(...)]
import itertools
a = [1,2]
b = [3,4]
f1 = 10
f2 = 20
f3 = 30
得到a
和b
的乘积
things = itertools.product(a,b)
使用固定值和乘积
构造结果
z = [(f1, f2, thing, f3) for thing in map(list, things)]
>>> for thing in z:
print thing
(10, 20, [1, 3], 30)
(10, 20, [1, 4], 30)
(10, 20, [2, 3], 30)
(10, 20, [2, 4], 30)
>>>
它不是通用的,它不会处理任意数量的固定事物和可迭代事物。
这是一个更通用的解决方案
def f(fixed, iterables):
things = itertools.product(*iterables)
last = fixed[-1]
for thing in things:
out = fixed[:-1]
out.extend((thing, last))
yield out
用法:
for thing in f([f1, f2, f3, f4], [a, b]):
print thing
我正在尝试做一个可迭代但有一些固定参数,并且可迭代参数在列表中。 这是我正在寻找的输出:
(fix1, fix2, fix3, [iterable1_1, iterable2_1, iterable3_1], fix4)
(fix1, fix2, fix3, [iterable1_1, iterable2_1, iterable3_2], fix4)
等基本上,只有列表中的三个发生变化;其余不变。
到目前为止,我已经试过了,但它并没有真正奏效。
iterable = itertools.product([fix1], [fix2], [fix3], [[iter1_1, iter1_2, iter1_3], [iter2_1, iter2_2], [iter3_1, iter3_2, iter3_3]], [fix4])
iter1、iter2 和 iter3 的长度不同,但我认为这不相关。
一个例子: 我有两个列表,a = [1,2] 和 b = [3,4] 以及一些固定参数 f1 = 10、f2=20、f3=30 所需的输出将是:
(10, 20, [1,3], 30)
(10, 20, [1,4], 30)
(10, 20, [2,3], 30)
(10, 20, [2,4], 30)
如有疑问,请创建函数。
def framed(*iters):
for pair in itertools.product(*iters):
yield (10, 20, pair, 30)
for result in framed([1, 2], [3, 4]):
print result
(10, 20, (1, 3), 30)
(10, 20, (1, 4), 30)
(10, 20, (2, 3), 30)
(10, 20, (2, 4), 30)
听起来你想要这样的东西:
result = [(fix1, fix2, fix3, [a, b, c], fix4)
for a, b, c in itertools.product(iterable1, iterable2, iterable3)]
如果带有a, b, c
的内部序列可以是元组而不是列表,则不需要解包:
result = [(fix1, fix2, fix3, prod, fix4) for prod in product(...)]
import itertools
a = [1,2]
b = [3,4]
f1 = 10
f2 = 20
f3 = 30
得到a
和b
things = itertools.product(a,b)
使用固定值和乘积
构造结果z = [(f1, f2, thing, f3) for thing in map(list, things)]
>>> for thing in z:
print thing
(10, 20, [1, 3], 30)
(10, 20, [1, 4], 30)
(10, 20, [2, 3], 30)
(10, 20, [2, 4], 30)
>>>
它不是通用的,它不会处理任意数量的固定事物和可迭代事物。
这是一个更通用的解决方案
def f(fixed, iterables):
things = itertools.product(*iterables)
last = fixed[-1]
for thing in things:
out = fixed[:-1]
out.extend((thing, last))
yield out
用法:
for thing in f([f1, f2, f3, f4], [a, b]):
print thing