anderson anderson - 2 months ago 6x
Python Question

possible unique combinations between between two arrays(of different size) in python?


gives us

I was wondering is there any way applying this to two different arrays.?I mean for arr1 and arr2.

Output should be OneThree,OneFour,TwoThree,TwoFour,FiveThree,FiveFour


You are looking for .product():

From the doc, it does this:

product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111

Sample code:

>>> x = itertools.product(arr1, arr2)
>>> for i in x: print i
('One', 'Three')
('One', 'Four')
('Two', 'Three')
('Two', 'Four')
('Five', 'Three')
('Five', 'Four')

To combine them:

# This is the full code
import itertools

arr1 = ['One','Two','Five']
arr2 = ['Three','Four']

combined = ["".join(x) for x in itertools.product(arr1, arr2)]