Reputation: 537
I would like to create a tuple which present all the possible pairs from two tuples
this is example for what I would like to receive :
first_tuple = (1, 2)
second_tuple = (4, 5)
mult_tuple(first_tuple, second_tuple)
output :
((1, 4), (4, 1), (1, 5), (5, 1), (2, 4), (4, 2), (2, 5), (5, 2))
This is what I did which succeed however look a bit cumbersome :
def mult_tuple(tuple1, tuple2):
ls=[]
for t1 in tuple1:
for t2 in tuple2:
c=(t1,t2)
d=(t2,t1)
ls.append(c)
ls.append(d)
return tuple(ls)
first_tuple = (1, 2)
second_tuple = (4, 5)
mult_tuple(first_tuple, second_tuple)
The code I wrote works , however I am looking for a nicer code
thank you in advance
Upvotes: 24
Views: 6495
Reputation: 22776
You can use itertools
's product
and permutations
:
from itertools import product, permutations
first_tuple, second_tuple = (1, 2), (4, 5)
result = ()
for tup in product(first_tuple, second_tuple):
result += (*permutations(tup),)
print(result)
Output:
((1, 4), (4, 1), (1, 5), (5, 1), (2, 4), (4, 2), (2, 5), (5, 2))
product
produces the tuples (two elements) produced equally by the nested for loop structure (your t1
and t2
variables), and permutations
produces the two permutations produced equally by your c
and d
variables.
Upvotes: 34
Reputation: 1721
My way in one line:
[item for sublist in [[(i,j),(j,i)] for i in first_tuple for j in second_tuple] for item in sublist]
[(1, 4), (4, 1), (1, 5), (5, 1), (2, 4), (4, 2), (2, 5), (5, 2)]
Upvotes: 0
Reputation: 71
def mul_tup(tup1, tup2):
l=[]
for x in tup1:
for y in tup2:
a=(x,y)
b=(y,x)
l.append(a)
l.append(b)
return tuple(l)
first_tup= tuple([eval(x) for x in input("enter the values: ").split(',')])
second_tup= tuple([eval(x) for x in input("enter the values: ").split(',')])
q = mult_tup(first_tup, second_tup)
print(q)
Upvotes: -1
Reputation: 109546
A one-liner using a list comprehension that doesn't require an import
.
t1 = (1, 2)
t2 = (4, 5)
>>> sorted([t for i in t1 for j in t2 for t in ((i, j), (j, i))])
# [(1, 4), (1, 5), (2, 4), (2, 5), (4, 1), (4, 2), (5, 1), (5, 2)]
Of course, for "all possible pairs from two tuples means" that you would have at most eight tuple pairs in the result. You could explicitly reference them, which should be the fastest solution if this is time critical code (and if sorting is not required, it will be faster still).
>>> sorted(((t1[0], t2[0]), (t1[0], t2[1]), (t1[1], t2[0]), (t1[1], t2[1]),
(t2[0], t1[0]), (t2[0], t1[1]), (t2[1], t1[0]), (t2[1], t1[1])))
# [(1, 4), (1, 5), (2, 4), (2, 5), (4, 1), (4, 2), (5, 1), (5, 2)]
Optional: Use set
to ensure only unique pairs are returned.
t1 = (1, 2)
t2 = (1, 2)
>>> sorted([t for i in t1 for j in t2 for t in ((i, j), (j, i))])
# [(1, 1), (1, 1), (1, 2), (1, 2), (2, 1), (2, 1), (2, 2), (2, 2)]
>>> sorted(set([t for i in t1 for j in t2 for t in ((i, j), (j, i))]))
# [(1, 1), (1, 2), (2, 1), (2, 2)]
Upvotes: 1
Reputation: 11602
Here is an ugly one-liner.
first_tuple = (1, 2)
second_tuple = (4, 5)
tups = [first_tuple, second_tuple]
res = [(i, j) for x in tups for y in tups for i in x for j in y if x is not y]
# [(1, 4), (1, 5), (2, 4), (2, 5), (4, 1), (4, 2), (5, 1), (5, 2)]
Unless you are using this for sport, you should probably go with a more readable solution, e.g. one by MrGeek below.
Upvotes: 12
Reputation: 195438
first_tuple = (1, 2)
second_tuple = (4, 5)
out = []
for val in first_tuple:
for val2 in second_tuple:
out.append((val, val2))
out.append((val2, val))
print(tuple(out))
Prints:
((1, 4), (4, 1), (1, 5), (5, 1), (2, 4), (4, 2), (2, 5), (5, 2))
Upvotes: 3
Reputation: 30920
Also You can do:
from itertools import permutations
t1=(1,2)
t2=(3,4)
my_tuple=tuple([key for key in filter(lambda x: x!=t1 and (x!=t2),list(permutations(t1+t2,2)))])
Upvotes: 2
Reputation: 531125
itertools.product
gives you what you want. However, since the Cartesian product of two tuples is not commutative (product(x,y) != product(y,x)
), you need to compute both and concatenate the results.
>>> from itertools import chain, product
>>> x = (1,4)
>>> y = (2, 5)
>>> list(chain(product(x,y), product(y,x)))
[(1, 2), (1, 5), (4, 2), (4, 5), (2, 1), (2, 4), (5, 1), (5, 4)]
(You can use chain
here instead of permutations
because there are only two permutations of a 2-tuple, which are easy enough to specify explicitly.)
Upvotes: 8
Reputation: 23176
If you’d like to avoid the use of the standard library (itertools
) then simply combine two list comprehensions:
result = [(x, y) for x in first_tuple for y in second_tuple]
result.extend( (x, y) for x in second_tuple for y in first_tuple )
then convert to a tuple
if it’s important to you.
Upvotes: 5