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

like itertools.combinations(arr1,2) gives us
('OneTwo','TwoFive','OneFive')
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

anderson
  • 107
  • 1
  • 3
  • 11
  • Possible duplicate of [Generating all unique pair permutations](http://stackoverflow.com/questions/14169122/generating-all-unique-pair-permutations) – Johan Lundberg Jul 23 '16 at 06:16

3 Answers3

2

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)]
Moon Cheesez
  • 2,489
  • 3
  • 24
  • 38
1

If all you wanted is OneThree,OneFour,TwoThree,TwoFour,FiveThree,FiveFour then a double for loop will do the trick for you:

>>> for x in arr1:
        for y in arr2:
            print(x+y)


OneThree
OneFour
TwoThree
TwoFour
FiveThree
FiveFour

Or if you want the result in a list:

>>> [x+y for x in arr1 for y in arr2]
['OneThree', 'OneFour', 'TwoThree', 'TwoFour', 'FiveThree', 'FiveFour']
Iron Fist
  • 10,739
  • 2
  • 18
  • 34
1
["".join(v) for v in itertools.product(arr1, arr2)]
#results in 
['OneThree', 'OneFour', 'TwoThree', 'TwoFour', 'FiveThree', 'FiveFour']
DurgaDatta
  • 3,952
  • 4
  • 27
  • 34