Home > Mobile >  How to create list of array combinations lexographically in numpy?
How to create list of array combinations lexographically in numpy?

Time:07-10

I have this array and I want to return unique array combinations. I tried meshgrid but it creates duplicates and inverse array values

>> import numpy as np
>> array = np.array([0,1,2,3])
>> combinations = np.array(np.meshgrid(array, array)).T.reshape(-1,2)
>> print(combinations)

[[0 0] 
 [0 1]
 [0 2]
 [0 3]
 [1 0]
 [1 1]
 [1 2]
 [1 3]
 [2 0]
 [2 1]
 [2 2]
 [2 3]
 [3 0]
 [3 1]
 [3 2]
 [3 3]]

What I want to exclude are the repeating arrays: [0,0] [1,1] [2,2] [3,3] and the inverse arrays when [2,3] is returned exclude [3,2] in the output.

Take a look at this combination calculator, this is the output that I like but how can I create it in NumPy?

CodePudding user response:

you could use combinations from itertools

import numpy as np
from itertools import combinations

array = np.array([0,1,2,3])
combs = np.array(list(combinations(arr, 2)))
  • Related