【问题标题】:All 6-Number Permutations from a List列表中的所有 6 位数排列
【发布时间】:2014-03-31 06:14:19
【问题描述】:

我正在编写一个程序,目标是获取一个数字列表并使用递归函数返回所有六个字母的组合(无需导入函数来为我执行此操作)。比如说,我的数字是“1 2 3 4 5 6 7 8 9”,输出将是:

1 2 3 4 5 6
1 2 3 4 5 7
1 2 3 4 5 8
1 2 3 4 5 9
1 2 3 4 6 7
1 2 3 4 6 8
1 2 3 4 6 9
1 2 3 4 7 8
... etcetera, all the way down to
4 5 6 7 8 9

我不是在寻找代码,而是在概念上朝着正确的方向推进。到目前为止我所做的尝试都失败了,我已经把自己逼入了一个合乎逻辑的轨道。

我在下面包含了我之前使用的代码,但它并不是真正的递归函数,似乎只适用于 6-8 位值。它非常混乱,我可以完全废弃它:

# Function prints all the possible 6-number combinations for a group of numbers
def lotto(constantnumbers, variablenumbers):
    # Base case: No more constant variables, or only 6 numbers to begin with
    if len(constantnumbers) == 0 or len(variablenumbers) == 0:    
        if len(constantnumbers) == 0:
            print(" ".join(variablenumbers[1:7]))
        else:
            print(" ".join(constantnumbers[0:6]))
        i = 6 - len(constantnumbers)
        outvars = variablenumbers[1:i + 1]
        if len(variablenumbers) > len(outvars) + 1:
            print(" ".join(constantnumbers + outvars))
            for index in range(len(outvars), 0, -1):
                outvars[index - 1] = variablenumbers[index + 1]
                print(" ".join(constantnumbers + outvars))
    else:
        i = 6 - len(constantnumbers)
        outvars = variablenumbers[1:i + 1]
        print(" ".join(constantnumbers + outvars))
        if len(variablenumbers) > len(outvars) + 1:
            for index in range(len(outvars), 0, -1):
                outvars[index - 1] = variablenumbers[index + 1]
                print(" ".join(constantnumbers + outvars))
        #Reiterates the function until there are no more constant numbers
        lotto(constantnumbers[0:-1], constantnumbers[-1:] + variablenumbers)

【问题讨论】:

  • 为什么使用字符串而不是整数?
  • 在问题的上下文中,这并不重要。从理论上讲,它也应该能够处理字母表中的字母。
  • 嗯,不,不是说。我只是不认为这会产生可读的代码。您可以通过在开头将数字转换为整数来使该函数对作为字符串输入的数字起作用。
  • 不管怎样,我更关心的是递归函数本身的结构以及我应该如何实现它。如果整数会使它更具可读性,那么一旦我真正知道我在做什么,我肯定会用它们来实现它。我仍然对函数应该如何工作感到迷茫。

标签: python list python-3.x recursion permutation


【解决方案1】:
import itertools

for combo in itertools.combinations(range(1,10), 6):
    print(" ".join(str(c) for c in combo))

给了

1 2 3 4 5 6
1 2 3 4 5 7
1 2 3 4 5 8
...
3 4 6 7 8 9
3 5 6 7 8 9
4 5 6 7 8 9

编辑:好的,这是一个递归定义:

def combinations(basis, howmany):
    for index in range(0, len(basis) - howmany + 1):
        if howmany == 1:
            yield [basis[index]]
        else:
            this, remainder = basis[index], basis[index+1:]
            for rest in combinations(remainder, howmany - 1):
                yield [this] + rest

编辑2:

基本情况:1 项组合是任何基本项。

归纳:N 项组合是任何基础项加上剩余基础的 (N-1) 项组合。

【讨论】:

  • 另外,我正在尝试使用我自己的函数而不是导入一个函数。
  • itertools 是标准 python 库的一部分。为什么不导入?
  • 没错。我正在尝试练习递归,我想看看它是如何为自己工作的。
  • 以我目前的编程经验,恐怕“产量”概念有点超出我的理解。我会找到递归调用应该如何工作的简单概述(即)基本情况是什么?,我如何迭代它?等)更有帮助。
  • @user3361007:现在没有时间学习了 ;-) 你可以将 yield 视为“返回一个值,然后在下次调用函数时从同一个位置继续”。它将您的函数转换为生成器,该生成器返回一系列值而不是值列表,这意味着您不必用尽内存来存储值,直到您准备好使用它们。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-15
  • 1970-01-01
  • 2013-05-01
相关资源
最近更新 更多