[英]Read vertically all the values of all the possible combinations of an unknown number of lists with different sizes in 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 人的任何規模。我見過這個問題的類似版本,但除了團隊規模可能不同之外,沒有一個版本。 我希望的結果是以這樣一種方式實現的,我可以調用一個函數,該函數接受一組團隊並返回一個整數作為團隊的分數,並跟蹤得分最高的團隊。
IE:
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_temmate):
'''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_temmate + 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_temmate + 1)] * i))):
if sum(x) == n and list(x) == sorted(x):
result.append(x)
return result
def team_generator(people, min_teammate, max_temmate):
'''Loop through all permutation then remove duplicate.'''
n = len(people)
patterns = length_to_split(n, min_teammate, max_temmate)
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_temmate = 5
g = team_generator(range(13), min_teammate, max_temmate)
for i in range(3):
print(next(g))
print()
g = team_generator(people, min_teammate, max_temmate)
for i in range(3):
print(next(g))
輸出:
if sublist != sorted(sublist):
break
else:
comb = [[people[i] for i in t] for t in comb]
yield comb
# Testing
g = team_generator(range(13), min_teammate, max_temmate)
for i in range(3):
print(next(g))
print()
g = team_generator(people, min_teammate, max_temmate)
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']]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.