我如何生成具有多列的数组的行的组合?

How could I generate a combinatorial of the lines of an array with several columns?

我想得到一个数组的n行所有可能的组合。 我不在乎它们是什么顺序,所以它不是排列。

示例:

我有一个数组:

[(1,2,3), (4,5,6), (7,8,9)]

问题: 我想找到两行的所有组合:

[(1,2,3), (4,5,6)]
[(1,2,3), (7,8,9)]
…

非常感谢!!

使用 itertools.combinations 并将组合转换为列表,参见 the docs:

from itertools import combinations

lst = [(1,2,3), (4,5,6), (7,8,9)]
for x in combinations(lst, 2):
    print(list(x))

结合 itertools 试试这个

import itertools
test = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
for i in range(0, len(test)):
    for j in sorted(np.arange(len(test)-1,i,-1)):
        print(test[i])
        print(test[j])
        print(list(itertools.product(test[i], test[j])))