Make all possible combinations of arrays


Lets say you have an multidimensional array

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

I want all the possible combinations of two arrays given the multidimensional array "a" so that:

[[1, 2],[3, 5]] , [[1, 2],[4,5]] ... 

I have no idea how to due this. Does someone have some suggestions and tips?

Asked By: Mathomat55



You can use itertools.combinations function defined in this answer. This code creates the list of all the combinations.

import numpy as np
import itertools

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

for L in range(len(a) + 1):
    for subset in itertools.combinations(a, L):
        combination.append([list(sub) for sub in subset])
Answered By: Enes Altınışık
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.