【问题标题】:Iterating through a list to produce all possible combinations of elements in a list?遍历列表以生成列表中所有可能的元素组合?
【发布时间】:2017-08-13 08:19:56
【问题描述】:

我正在尝试打印出列表中所有可能的元素组合。

import random

def fun(lst, run):
    i = 0
    while i < run:
        newList = lst
        NewNumbers = newList[-1:] + newList[:-1] #shifts each element in the to the right
        lst = NewNumbers
        print(lst)
        i += 1

fun([1, 2, 0], 3)

作为初始列表 [1, 2, 0]。这个程序产生输出

>>>>>>>>
[0, 1, 2]
[2, 0, 1]
[1, 2, 0]
>>>>>>>>

我不得不将列表从 [1, 2, 0] 实际更改为 [1, 1, 0] 之类的其他内容,以获得其他可能的组合

>>>>>>>>
[0, 1, 1]
[1, 0, 1]
[1, 1, 0]
>>>>>>>>

然后继续将列表更改为[2, 2, 0], [0, 0, 2] 等以获得其他组合,一旦我将列表增加到 4 个元素,例如[1, 2, 0, 1],这非常耗时且不容易做到

我已经能够找到一种方法来使用 python 的 intertools 来做到这一点

import itertools
def fun(lst):
        all_possible_combinations = set(itertools.product(lst, repeat=3)) #repeat = number of elements
        return all_possible_combinations
print(fun([0, 1, 2]))

这正是我所寻找的,它生成元素 0、1、2 的所有可能组合类型

{(0, 1, 1), (0, 1, 2), (1, 0, 0), (1, 0, 1), (0, 2, 1), (1, 0, 2), (0, 2, 0), (0, 2, 2), (2, 0, 1), (1, 2, 0), (2, 0, 0), (1, 2, 1), (0, 0, 2), (1, 2, 2), (2, 0, 2), (0, 0, 1), (0, 0, 0), (2, 1, 2), (1, 1, 1), (1, 1, 0), (2, 2, 2), (2, 1, 0), (2, 2, 1), (2, 1, 1), (1, 1, 2), (2, 2, 0), (0, 1, 0)}

我正在尝试通过一个循环来生成所有这些组合,该循环通过迭代,例如第一次迭代 (0, 1, 1) 然后第二次迭代 (0, 1, 2) 如下所示:

(0, 1, 1)
(0, 1, 2) 
(1, 0, 0)
(1, 0, 1) 

【问题讨论】:

    标签: python list loops while-loop


    【解决方案1】:

    文档中显示了与 itertools.product() 等效的纯 python:

    def product(*args, **kwds):
        # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
        # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
        pools = map(tuple, args) * kwds.get('repeat', 1)
        result = [[]]
        for pool in pools:
            result = [x+[y] for x in result for y in pool]
        for prod in result:
            yield tuple(prod)
    

    【讨论】:

      【解决方案2】:

      您可以在此处查看 itertools 产品的代码:https://docs.python.org/3/library/itertools.html#itertools.product

      如果您希望您的函数与您的函数具有相同的变量名称,这里有一个产品代码的修改版本来做您想做的事情:

      def fun(lst, run):
          pools = [lst] * run
          result = [[]]
          for pool in pools:
              result = [x+[y] for x in result for y in pool]
          for prod in result:
              yield(tuple(prod))
      
      print(list(fun([1, 2, 0], 3)))
      

      输出:

      [(1, 1, 1), (1, 1, 2), (1, 1, 0), (1, 2, 1), (1, 2, 2), (1, 2, 0), (1, 0, 1), (1, 0, 2), (1, 0, 0), (2, 1, 1), (2, 1, 2), (2, 1, 0), (2, 2, 1), (2, 2, 2), (2, 2, 0), (2, 0, 1), (2, 0, 2), (2, 0, 0), (0, 1, 1), (0, 1, 2), (0, 1, 0), (0, 2, 1), (0, 2, 2), (0, 2, 0), (0, 0, 1), (0, 0, 2), (0, 0, 0)]
      

      【讨论】:

        【解决方案3】:

        此方法使用递归函数生成所有组合的列表,然后您可以对其进行迭代。

        def product(lst, current, rslt):
            if len(current) >= len(lst) - 1:
                for item in lst:
                    rslt += [current + [item]]
            else:
                for item in lst:
                    product(lst, current + [item], rslt)
        
        rslt = []
        product([0, 1, 2], [], rslt)
        for p in rslt:
            print p
        

        【讨论】:

        • 问题已经包含这个答案,他们正在尝试用循环来做
        猜你喜欢
        • 1970-01-01
        • 2023-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-14
        相关资源
        最近更新 更多