Python 不同规模团队的所有可能组合

Python all possible group combinations of teams with different sizes

我正在尝试从一群人中组建每一个不同规模的独特团队。我有一个大小为 n 的人员列表,并且有 k 个团队。在下面的案例中有 13 个人和 4 个团队。

people = ["Bob", "Jane", "Mary", "Martha", "James", "Charles", "Kevin", "Debbie", "Brian", "Matt", "Milo", "Chris", "Sam"]

示例输出:

[[Bob, Jane, Mary], [Martha, James, Charles], [Kevin, Debbie, Brian], [Matt, Milo, Chris, Sam]]
[[Bob, Jane, Mary], [Martha, James, Charles], [Kevin, Debbie, Matt], [Brian, Milo, Chris, Sam]]
[[Bob, Jane, Mary], [Martha, James, Charles], [Kevin, Debbie, Milo], [Brain, Matt, Chris, Sam]]
[[Bob, Jane, Mary], [Martha, James, Charles], [Kevin, Debbie, Chris], [Brian, Matt, Milo, Sam]]
.
.
.
[[Bob, Jane, Mary], [Martha, James, Charles], [Kevin, Debbie, Brian, Matt], [Milo, Chris, Sam]]
.
.
.

团队可以是 3 - 5 人的任何规模。我见过类似版本的这个问题,但 none 另外团队的规模可以不同。我希望的结果是以这样一种方式实现的,我可以调用一个函数,该函数接受一组团队和 returns 一个整数作为团队的分数,并跟踪得分最高的团队.

即:

def generate_best_teams(people, num_teams):
    loop:
        teams gets created
        teams_score = calculate_score(teams)
        if teams_score > best_score:
            best_score = teams_score
            best_teams = teams

    return best_teams

任何解决此问题的帮助将不胜感激。

如果你像这里一样有 13 个人,你会使用三人小组,直到你需要四人小组为其他人服务。现在你必须创建一个算法来创建三到五个团队。我认为每个团队成员的这种变化是为了解决人们无法适应三人团队的问题。我会创建三人团队,直到我需要四人或五人团队。您也可以创建各种规模的所有团队,但这适用于人数过多的团队。

我试图制作一个生成器来产生结果。我很确定有更好的方法来做到这一点。看看是否有效:

from itertools import permutations, islice, product

def length_to_split(n, min_teammate, max_teammate):
    '''Return a list of pattern, e.g.

    >>> length_to_split(13, 3, 5)
    [(3, 5, 5), (4, 4, 5), (3, 3, 3, 4)]
    '''
    min_number_of_group = n // max_teammate + 1
    max_number_of_group = n // min_teammate

    result = []
    for i in range(min_number_of_group, max_number_of_group + 1):
        for x in list(product(*([range(min_teammate, max_teammate + 1)] * i))):
            if sum(x) == n and list(x) == sorted(x):
                result.append(x)
    return result

def team_generator(people, min_teammate, max_teammate):
    '''Loop through all permutation then remove duplicate.'''
    n = len(people)
    patterns = length_to_split(n, min_teammate, max_teammate)
    for p in patterns:
        for comb in permutations(range(n), n):
            input = iter(comb)
            comb = [list(islice(input, elem)) for elem in p]
            
            # Remove duplicate permutation
            for sublist in comb:
                if sublist != sorted(sublist):
                    break
            else:
                comb = [[people[i] for i in t] for t in comb]
                yield comb

# Testing
people = ["Bob", "Jane", "Mary", "Martha", "James", "Charles", "Kevin", "Debbie", "Brian", "Matt", "Milo", "Chris", "Sam"]
min_teammate = 3
max_teammate = 5

g = team_generator(range(13), min_teammate, max_teammate)
for i in range(3):
    print(next(g))

print()

g = team_generator(people, min_teammate, max_teammate)
for i in range(3):
    print(next(g))

输出:

[[0, 1, 2], [3, 4, 5, 6, 7], [8, 9, 10, 11, 12]]
[[0, 1, 2], [3, 4, 5, 6, 8], [7, 9, 10, 11, 12]]
[[0, 1, 2], [3, 4, 5, 6, 9], [7, 8, 10, 11, 12]]

[['Bob', 'Jane', 'Mary'], ['Martha', 'James', 'Charles', 'Kevin', 'Debbie'], ['Brian', 'Matt', 'Milo', 'Chris', 'Sam']]
[['Bob', 'Jane', 'Mary'], ['Martha', 'James', 'Charles', 'Kevin', 'Brian'], ['Debbie', 'Matt', 'Milo', 'Chris', 'Sam']]
[['Bob', 'Jane', 'Mary'], ['Martha', 'James', 'Charles', 'Kevin', 'Matt'], ['Debbie', 'Brian', 'Milo', 'Chris', 'Sam']]

已编辑:修正了代码中的一些拼写错误。