有没有更好的方法将多个参数发送到 itertools.product?
Is there a better way to send multiple arguments to itertools.product?
我正在尝试从包含多行的二维列表创建 itertools.product。例如,考虑一个列表 s:
[[0.7168573116730971,
1.3404415914042531,
1.8714268721791336,
11.553051251803975],
[0.6702207957021266,
1.2476179147860895,
1.7329576877705954,
10.635778602978927],
[0.6238089573930448,
1.1553051251803976,
1.5953667904468385,
9.725277699842893],
[0.5776525625901988,
1.0635778602978927,
1.4587916549764335,
8.822689900641748]]
我想在列表的 4 行之间计算 itertools.product:
pr = []
for j in (it.product(s[0],s[1],s[2],s[3])):
pr.append(j)
这给出了 pr 的必要结果,其维度为 256,4,其中 256 是(列数 ^ 行数)。但是,有没有更好的方法将 list 的所有行作为参数发送而不必写每一行的名称。如果要为更大的列表完成这将很烦人。
我想如果 s 是 numpy.array 就可以使用 numpy.meshgrid。但即使在那里,我也必须逐行记下作为参数。
您可以使用 Python 中的解包符号 *
为此:
import itertools as it
s = [[0.7168573116730971,
1.3404415914042531,
1.8714268721791336,
11.553051251803975],
[0.6702207957021266,
1.2476179147860895,
1.7329576877705954,
10.635778602978927],
[0.6238089573930448,
1.1553051251803976,
1.5953667904468385,
9.725277699842893],
[0.5776525625901988,
1.0635778602978927,
1.4587916549764335,
8.822689900641748]]
pr = []
for j in (it.product(*s):
pr.append(j)
它会将列表中的每一项 s
发送到 product
函数
我正在尝试从包含多行的二维列表创建 itertools.product。例如,考虑一个列表 s:
[[0.7168573116730971,
1.3404415914042531,
1.8714268721791336,
11.553051251803975],
[0.6702207957021266,
1.2476179147860895,
1.7329576877705954,
10.635778602978927],
[0.6238089573930448,
1.1553051251803976,
1.5953667904468385,
9.725277699842893],
[0.5776525625901988,
1.0635778602978927,
1.4587916549764335,
8.822689900641748]]
我想在列表的 4 行之间计算 itertools.product:
pr = []
for j in (it.product(s[0],s[1],s[2],s[3])):
pr.append(j)
这给出了 pr 的必要结果,其维度为 256,4,其中 256 是(列数 ^ 行数)。但是,有没有更好的方法将 list 的所有行作为参数发送而不必写每一行的名称。如果要为更大的列表完成这将很烦人。
我想如果 s 是 numpy.array 就可以使用 numpy.meshgrid。但即使在那里,我也必须逐行记下作为参数。
您可以使用 Python 中的解包符号 *
为此:
import itertools as it
s = [[0.7168573116730971,
1.3404415914042531,
1.8714268721791336,
11.553051251803975],
[0.6702207957021266,
1.2476179147860895,
1.7329576877705954,
10.635778602978927],
[0.6238089573930448,
1.1553051251803976,
1.5953667904468385,
9.725277699842893],
[0.5776525625901988,
1.0635778602978927,
1.4587916549764335,
8.822689900641748]]
pr = []
for j in (it.product(*s):
pr.append(j)
它会将列表中的每一项 s
发送到 product
函数