Home > other >  Create an array in python from two different arrays with all possible combinations with having fixed
Create an array in python from two different arrays with all possible combinations with having fixed

Time:04-22

I am trying to get the combinations of the two arrays with a fixed selection from each array.

Arrays:

X = ['A','B','C','D','E']
Y = ['1','2','3','4']

My condition for selection will be Sx = 3 and Sy = 2 (the output should have 3 elements from X and 2 elements from Y which are fixed)

The output should be similar to this with all possible combinations

XY = [('A','B','C','1','2'),('B','C','D','2','3'),....)]

How can I do that?

CodePudding user response:

use itertools

combos1 = itertools.combinations(X,r=Sx)
combos2 = itertools.combinations(Y,r=Sy)
prod1 = [a b for a,b in itertools.product(combos1,combos2)]

this is computationally expensive ... it may take a while for big alphabets

  • Related