简体   繁体   中英

create a tuple from pairs

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

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.

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, eg one by MrGeek below.

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.)

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.

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)))])
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))

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)]

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)]
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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM