Python 将非常手动的函数重构为通用函数

Python refactoring a very manual function into a generalized function

我正在从事一个产生最佳团队的项目。我只是要暴力破解它并生成所有可能的人组合。到目前为止我有这个:

from itertools import combinations
def testing(people, people_copy, teams):
    for team in teams:
        people_copy = people.copy()
        for comb in list(combinations(people_copy, team[0])):
            people_copy = people.copy()
            for i in comb:
                people_copy.remove(i)
            for combi in list(combinations(people_copy, team[1])):
                people_copy = people.copy()
                for i in comb:
                    people_copy.remove(i)
                for j in combi:
                    people_copy.remove(j)
                for combin in list(combinations(people_copy, team[2])):
                    people_copy = people.copy()
                    for i in comb:
                        people_copy.remove(i)
                    for j in combi:
                        people_copy.remove(j)
                    for k in combin:
                        people_copy.remove(k)
                    for combina in list(combinations(people_copy, team[3])):
                        yield [list(comb), list(combi), list(combin), list(combina)]

其中 people 是一个整数列表,即 [1, 2, 3, 4, 5, ... n),teams 是一个列表列表,其中包含团队应该有多大,即 [[3, 3 , 5, 5], [3, 4, 4, 5]]。在这两种情况下,都会创建 3 个团队。第一种情况,第一队3人,第二队3人,第三队5人,第四队5人。同样在第二个名单中,第一队3人,第二队4人,第三队4人,第四队5人。 正如您在我的函数中看到的那样,我正在执行一个非常手动的过程(编写它是为了测试这个概念)。我想知道如何重构此函数以接受 k 的团队大小,即 len(teams[0]) = k.

我对我的递归技能没有信心,但它确实有效,所以我认为推动是朝着好的方向发展的。只需用 [3, 3, 5, 5] 而不是 [[3, 3, 5, 5], [3, 4, 4, 5]]

这样的子团队来喂它
def testing(people, teams, result=[]):
    if not teams:
        yield result
        return
    new_team=teams.copy()
    team=new_team.pop(0)
    people_team=people.copy()
    for comb in combinations(people, team):
        people_comb=people_team.copy()
        for val in comb:
            people_comb.remove(val)
        new_result=result.copy()
        new_result.append(comb)
        yield from testing(people_comb, new_team, new_result)