【问题标题】:create a tuple from pairs从对创建一个元组
【发布时间】:2019-08-11 20:34:01
【问题描述】:

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

这是我想收到的示例:

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)  

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

【问题讨论】:

  • itertools.product 会让你走到一半。
  • 如果您正在寻找 unique 元组,您可能还需要考虑。是否要同时添加 (1, 1) 和它的反向?
  • 我认为这个问题更适合Code Review SE

标签: python tuples


【解决方案1】:
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))

【讨论】:

    【解决方案2】:

    你可以使用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 产生由您的 c 和 @ 产生的两个排列987654334@变量。

    【讨论】:

      【解决方案3】:

      这是一个丑陋的单线。

      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 的一个。

      【讨论】:

        【解决方案4】:

        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 元组只有两个排列,很容易显式指定。)

        【讨论】:

        • 您还可以解压缩可迭代对象,以一种可以说更简洁的方式生成最终列表:[*product(x, y), *product(y, x)]
        【解决方案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

        【讨论】:

          【解决方案6】:

          你也可以这样做:

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

          【讨论】:

          • 投反对票,因为这明确依赖于长度为 2 的 t1t2,否则会产生不正确的输出。
          【解决方案7】:

          使用不需要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)]
          

          【讨论】:

            【解决方案8】:
            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)
            

            【讨论】:

              【解决方案9】:

              我的方式在一行中:

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

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2016-01-05
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2013-09-10
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多