pandas 从两个团队的22名玩家中创建一个由11名玩家组成的团队

p5cysglq  于 2023-04-04  发布在  其他
关注(0)|答案(1)|浏览(132)

我是python的新手,我试图为一个语句创建一个解决方案。
1.有两个队在比赛。
1.每个队有11名球员,所以总共有22名球员。
1.在22名玩家中,只需要选择11名玩家。
1.现在从11名入选的球员中,一人可以成为队长,一人可以成为副队长。
我们可以从excel中读取球员的名字。
解决方案是输入所有22名球员的名字,并显示所有球队(11名球员)与队长和副队长。
在这里,我能够生成随机队的22名球员,但上述限制得到匹配。

import random

def get_random_player(players):
    return players.pop(random.randrange(len(players)))

team_1 = []
players = ["Rahul","Virat","Sachin","King"] //22 players

for x in range(len(players) // 2):
   team_1.append(get_random_player(players))

print(team_1)

示例:

Team 1 :
Team1_Palyer1
Team1_Palyer2
Team1_Palyer3
Team1_Palyer4
Team1_Palyer5
Team1_Palyer6
Team1_Palyer7
Team1_Palyer8
Team1_Palyer9
Team1_Palyer10
Team1_Palyer11

Team 2 :
Team2_Palyer1
Team2_Palyer2
Team2_Palyer3
Team2_Palyer4
Team2_Palyer5
Team2_Palyer6
Team2_Palyer7
Team2_Palyer8
Team2_Palyer9
Team2_Palyer10
Team2_Palyer11

Output :

Team1_Palyer1
Team2_Palyer1
Team1_Palyer3(c)
Team2_Palyer5
Team1_Palyer5
Team2_Palyer6
Team2_Palyer7(vc)
Team1_Palyer4
Team2_Palyer9
Team2_Palyer10
Team1_Palyer10

在这里,我们有5个plyers从队1和6名球员从队2作出Team1_Palyer3作为队长和Team2_Palyer7作为副队长。
需要显示可以创建的所有团队。

axr492tv

axr492tv1#

至于要创建多少个11名球员的球队可以从22名球员创建。你可以使用公式*组合多少球队将被创建

22C11 = 22! / (11! * (22-11)!) = 705,432

将生成的团队总数705,432

代码:

import itertools

players = ['Team1_Player1', 'Team1_Player2', 'Team1_Player3', 'Team1_Player4', 'Team1_Player5', 'Team1_Player6', 'Team1_Player7', 'Team1_Player8', 'Team1_Player9', 'Team1_Player10', 'Team1_Player11', 'Team2_Player1', 'Team2_Player2', 'Team2_Player3', 'Team2_Player4', 'Team2_Player5', 'Team2_Player6', 'Team2_Player7', 'Team2_Player8', 'Team2_Player9', 'Team2_Player10', 'Team2_Player11']

team_combinations=list(itertools.combinations(players, 11))
print(len(team_combinations)) #705432

#To print the team.
"""
for i,team in enumerate(team_combinations, start=1):
    print(f"Team {i}:")
    for player in team:
        print(player)
"""

注意只是没有明确你是如何创建你的团队的队长副队长的,因此它是缺失的。

相关问题