Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading