0

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!!

Stphane
  • 3,368
  • 5
  • 32
  • 47

2 Answers2

0

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))
gstukelj
  • 2,291
  • 1
  • 7
  • 20
0

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])))
Mike
  • 51
  • 5