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

35 Views Asked by At

I want to get all possible combinations of n lines of an array. I don't care what order they are, so it's not a permutacion.

Example:

I have an array:

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

Question: I want to find all combinations of two lines:

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

Thank you so much!!

2

There are 2 best solutions below

0
gstukelj On BEST ANSWER

Use itertools.combinations and cast the combinations to a list, see 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))
0
Mike On

Try this in combination with 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])))