我正在尝试创建所有可能的球员配对组合,根据A、B、C或D型障碍分配到4人高尔夫球队
我尝试过各种itertools方法,比如组合和排列,但都没有找到正确的方法。你知道吗
from itertools import combinations, product, permutations
g = player_df.groupby(by = 'hcp_ABCD')
teams_listoflists = [group[1].index for group in g]
teams_combo_ndx = [player for player in permutations(teams_listoflists, 4)]
这是我的熊猫桌:
handicap name hcp_ABCD
0 24 Player1 D
1 21 Player2 D
2 8 Player3 B
3 14 Player4 C
4 20 Player5 D
5 13 Player6 C
6 -1 Player7 A
7 5 Player8 A
8 8 Player9 B
9 6 Player10 B
10 20 Player11 D
11 15 Player12 C
12 0 Player13 A
13 12 Player14 C
14 0 Player15 A
15 10 Player16 B
我想输出的所有组合(没有重复)的球员组合(队),这样每个队有一个类型a,B,C,和D的每一个。此输出可以是一个类似于上面按“选项”分组的表
编辑: 为了清楚起见,我添加了这个输出示例。你知道吗
A Player B Player C Player D Player
option 1 team1 Player7 Player3 Player4 Player1
team2 Player8 Player9 Player6 Player2
team3 Player13 Player10 Player12 Player5
team4 Player15 Player16 Player14 Player11
option 2 team1 Player7 Player16 Player4 Player1
team2 Player8 Player3 Player6 Player2
team3 Player13 Player9 Player12 Player5
team4 Player15 Player10 Player14 Player11
...
A Player B Player C Player D Player
option n team1 Player7 Player3 Player4 Player11
team2 Player8 Player9 Player6 Player1
team3 Player13 Player10 Player12 Player2
team4 Player15 Player16 Player14 Player5
上面的观点是,我试图找到一个生成器,它循环遍历每个残障组中所有球员的组合,这样就可以清楚地看到球队的选择组合。你知道吗
编辑#2 我已确定此代码生成所有潜在团队组合的组合:
g = df.groupby(by = 'hcp_ABCD')
combinations = [list(group[1].index) for group in g]
这将创建一个列表,其中a个玩家在列表[0]中,B个玩家在列表[1]中,等等
这会得到所有可能的团队组合的索引器:
from itertools import product
options = [option for option in product(*combinations)]
但是,如何将这些分配到“选项”(见上面的例子)中并确保没有重复是我一直坚持的。你知道吗
编辑#3更简单的版本(思考此问题的方式)是使用以下集合:
A = ['A1', 'A2', 'A3', 'A4']
B = ['B1', 'B2', 'B3', 'B4']
C = ['C1', 'C2', 'C3', 'C4']
D= ['D1', 'D2', 'D3', 'D4']
这基本上与groupby在上面所做的相同(按hcp\u ABCD分组),但将每个人命名为“A Player”、“B Player”等
可能的团队组合:
team_combinations = [team for team in product(A, B, C, D)]
然后,下一个技巧是将这些分配到4个团队的组合中,没有重复的玩家。你知道吗
下面的方法是使用笛卡尔积,然后分组两次,将玩家分配到具有一组独特障碍的团队中。你知道吗
感谢您澄清预期结果。这是我测试的答案。它可能不是您预期结果的确切格式,但我让您来解决它。你知道吗
我在评论中提出了一个建议。下面是一个实现:
输出:
相关问题 更多 >
编程相关推荐