从成对创建元组

时间:2019-08-11 20:34:01

标签: python tuples

我想创建一个元组,以显示两个元组中所有可能的对

这是我想收到的示例:

first_tuple = (1, 2)
second_tuple = (4, 5)
mult_tuple(first_tuple, second_tuple)

输出:

((1, 4), (4, 1), (1, 5), (5, 1), (2, 4), (4, 2), (2, 5), (5, 2))

这就是我所做的,但是成功看起来有些麻烦:

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)  

我编写的代码有效,但是我正在寻找更好的代码
预先谢谢你

9 个答案:

答案 0 :(得分:34)

您可以使用itertoolsproductpermutations

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)

输出:

((1, 4), (4, 1), (1, 5), (5, 1), (2, 4), (4, 2), (2, 5), (5, 2))

product生成由嵌套的for循环结构(您的t1t2变量)平均生成的元组(两个元素),而permutations生成生成的两个置换同样地通过您的cd变量。

答案 1 :(得分:11)

这是一条难看的单线纸。

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

除非您将其用于运动,否则可能应该使用更具可读性的解决方案,例如下面是MrGeek的一份。

答案 2 :(得分:8)

itertools.product给您您想要的。但是,由于两个元组的笛卡尔积不是可交换的(product(x,y) != product(y,x)),因此您需要同时计算两者并连接结果。

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

(您可以在此处使用chain而不是permutations,因为2元组只有两个排列,可以很容易地明确指定。)

答案 3 :(得分:5)

如果您想避免使用标准库(itertools),则只需结合两个列表理解:

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 )

如果对您很重要,请转换为tuple

答案 4 :(得分:2)

还可以:

outline

答案 5 :(得分: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))

打印:

((1, 4), (4, 1), (1, 5), (5, 1), (2, 4), (4, 2), (2, 5), (5, 2))

答案 6 :(得分:1)

使用列表推导的单行代码,不需要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)]

当然,对于“来自两个元组的所有可能的对均值”,结果中最多具有八个元组对。您可以显式地引用它们,如果这是时间紧迫的代码,它应该是最快的解决方案(并且如果不需要排序,它将仍然更快)。

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

可选:使用set确保仅返回唯一对。

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

答案 7 :(得分:0)

我在一行中的方式:

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

答案 8 :(得分:-1)

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)