Make all possible combinations of arrays

Viewed 39

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?

1 Answers

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]])
combination=[]  

for L in range(len(a) + 1):
    for subset in itertools.combinations(a, L):
        combination.append([list(sub) for sub in subset])
combination 
Related