【问题标题】:combine each element of two lists to a tuple in Python without using itertools packet在 Python 中将两个列表的每个元素组合成一个元组,而不使用 itertools 包
【发布时间】:2021-10-08 09:28:01
【问题描述】:

我是新手,我有两个列表,想通过随机所有可能的元素将它们组合成一个元组,不使用任何数据包,例如 itertools 数据包。 像这个例子:

list1 = ["a", "b", "c"]
list2 = ["wow", 2]

和输出:

>>> new_tuple = (["a","wow"],["b","wow"],["c","wow"],["a",2],["b",2],["c",2])

你能帮帮我吗?提前谢谢你

【问题讨论】:

  • 一个双循环就足够了

标签: python arraylist tuples


【解决方案1】:

使用列表生成器的 Python3 单线

list1 = ['a', 'b', 'c']
list2 = ['wow', 2]
new_tuple = tuple([l1, l2] for l2 in list2 for l1 in list1)

print(new_tuple)
# (['a', 'wow'], ['b', 'wow'], ['c', 'wow'], ['a', 2], ['b', 2], ['c', 2])

【讨论】:

  • 输出是一个列表,但是我需要一个元组,我可以在那里使用 tuple() 吗?
  • 是的,你可以。已根据您的要求更新了 sn-p。
【解决方案2】:

你可以使用 itertools

import itertools
list1 = ["a", "b", "c"]
list2 = ["wow", 2]
c = tuple(itertools.product(list1, list2))
print(c)

【讨论】:

    【解决方案3】:

    您可以使用itertools 来进行您请求的排列,如下所示:

    import itertools
    list1 = ["a", "b", "c"]
    list2 = ["wow", 2]
    
    all_combinations = []
    
    list1_permutations = itertools.permutations(list1, len(list2))
    
    for each_permutation in list1_permutations:
        zipped = zip(each_permutation, list2)
        all_combinations.append(list(zipped))
    
    print(all_combinations)
    

    输出如下所示:

    [[('a', 'wow'), ('b', 2)], [('a', 'wow'), ('c', 2)], [('b', 'wow'), ('a', 2)], [('b', 'wow'), ('c', 2)], [('c', 'wow'), ('a', 2)], [('c', 'wow'), ('b', 2)]]```
    

    【讨论】:

    • 谢谢,如果我希望 list3 是一个元组,我可以使用 list3 = ([x, y] for x in list1 for y in list2) 吗?
    • 否,因为这不会提供您要求的所有可能性。但是你可以拨打tuple(all_combinations)
    • 实际上我不想使用任何数据包,但 Numpy。我还可以使用 for 循环将元素添加到元组中吗?
    【解决方案4】:
    import random
    
    list1 = ["a", "b", "c"]
    list2 = ["wow", 2]
    new_tuple = ()
    for x in (len(list1)+len(list2)):
      randInt = random.randint(0, len(list1))
      randInt2 = random.randint(0, len(list2))
      new_tuple += [list1[randInt], list2[randInt2]]
    

    这是一个非常简单的python。试试代码。

    【讨论】:

      【解决方案5】:

      您想要的基本上是笛卡尔积。尝试使用 itertools 进行列表推导,例如:

      import itertools
      list1 = ["a", "b", "c"]
      list2 = ["wow", 2]
      
      new_tuple = [x for x in itertools.product(list1, list2)]
      

      【讨论】:

      • 谢谢,但我不想在我的示例中使用 itertools 数据包。
      【解决方案6】:

      使用itertools.product:

      from itertools import product
      
      new_tuple = tuple([a, b] for b, a in product(list2, list1))
      # (['a', 'wow'], ['b', 'wow'], ['c', 'wow'], ['a', 2], ['b', 2], ['c', 2])
      

      【讨论】:

        【解决方案7】:

        为了将2个列表转换为元组,有.zip的方法:

        list(zip(list1, list2))
        

        但它只是在你的情况下添加元素: (["a", "wow"], ["b", "2"]], 所以:

        a = ("John", "Charles", "Mike")
        b = ("Jenny", "Christy", "Monica")
        

        tuples = []
        def combineTuples(listA, listB):
          for i in range(len(listA)):
            for j in range(len(listB)):
                tuples.append((a[i], b[j]))
                
        x = combineTuples(a, b)
        
        
        print(tuples)
        

        输出: [('John', 'Jenny'), ('John', 'Christy'), ('John', 'Monica'), ('Charles', 'Jenny'), ('Charles', 'Christy') , ('Charles', 'Monica'), ('Mike', 'Jenny'), ('Mike', 'Christy'), ('Mike', 'Monica')]

        与您的数据的输出是相同的,但没有顺序。

        [('a', 'wow'), ('a', 2), ('b', 'wow'), ('b', 2), ('c', 'wow'), ('c', 2)]

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-10-30
          • 1970-01-01
          • 2018-10-05
          • 1970-01-01
          • 2022-06-15
          • 1970-01-01
          相关资源
          最近更新 更多