带有步骤的python itertools产品

问题描述 投票:0回答:3

当前,我有一个NumPy数组:

a = np.array([[1,2],[3,4],[5,6]])

并且我正在itertools中进行产品操作,但是我想添加一个步进函数。例如:

[ x for x in itertools.product(*a) ]

结果:

[(1, 3, 5), (1, 3, 6), (1, 4, 5), (1, 4, 6), (2, 3, 5), (2, 3, 6), (2, 4, 5), (2, 4, 6)]

对于步骤(1,1,2)的目标输出:

[(1, 3, 5), (1, 4, 5), (2, 3, 5), (2, 4, 5)]

我不知道,这是问题所在。

python numpy itertools
3个回答
1
投票

1
投票
可能也有itertools工具来执行此操作。

In [586]: list(itertools.islice(itertools.product(*a), None, None,2)) Out[586]: [(1, 3, 5), (1, 4, 5), (2, 3, 5), (2, 4, 5)]


0
投票
print(list(itertools.product(* [[1,2],[3,4],[5,6]]))[:: 2])
© www.soinside.com 2019 - 2024. All rights reserved.