I'm trying to create all of the possible combinations of pairings of players to assign into 4 person golf teams based on handicap type A, B, C, or D.
I've tried various itertools methods such as combinations and permutations but can't figure out the right approach.
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)]
Here is my pandas table:
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
i would like the output to be all combinations (without duplicates) of player combinations (teams) such that each team has a type A, B, C, and D on each. This output can be a similar table as above grouped by "options."
Edit: Am adding this output example for clarity.
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
The point of the above is that I'm trying to find a generator that cycles through all combinations of player in each handicap group so that the combination of options of teams is clear.
Edit #2 I've determined that this code produces a combination of all of the potential team combinations:
g = df.groupby(by = 'hcp_ABCD')
combinations = [list(group[1].index) for group in g]
This creates a list of lists with the A Players in list[0], B Players in list[1], etc.
And this gets an indexer for all possible combinations of teams:
from itertools import product
options = [option for option in product(*combinations)]
But, how to assign these out into the "options" (see above example) and ensure no duplication is what I'm stuck on.
Edit #3 A simpler version (way to think about this problems) is to use the following sets:
A = ['A1', 'A2', 'A3', 'A4']
B = ['B1', 'B2', 'B3', 'B4']
C = ['C1', 'C2', 'C3', 'C4']
D= ['D1', 'D2', 'D3', 'D4']
This essentially does what the groupby does above (grouping by hcp_ABCD) but names each "A Player", "B Player", etc.
possible_combinations of teams:
team_combinations = [team for team in product(A, B, C, D)]
then the next trick is to assign these onto combinations of 4 teams with no duplication of players.