Marcin Sommer
Marcin Sommer

Reputation: 23

How to make all possible pairing for 2 teams of 3 players

Hi im new here and its my first question :)

Im trying to make algorithm to make all possible sets of pairs of 2 teams (3 players each) [3v3] if i make math right there should be 6 possible options (3!).

TeamA = [p1, p2, p3]
TeamB = [o1, o2, o3]

set1 = [[p1, o1], [p2, o2], [p3, o3]]
set2 = [[p1, o2], [p2, o3], [p3, o1]]
set3 = [[p1, o3], [p2, o1], [p3, o2]]
set4 = [[p1, o1], [p2, o3], [p3, o2]]
set5 = [[p1, o3], [p2, o2], [p3, o1]]
set6 = [[p1, o2], [p2, o1], [p3, o3]]

Is there a way to make it with algorithm? im want to implement it in python/django

Upvotes: 0

Views: 112

Answers (2)

Almog-at-Nailo
Almog-at-Nailo

Reputation: 1182

This could be achieved with itertools.permutations and zip:

from itertools import permutations

TeamA = ['p1', 'p2', 'p3']
TeamB = ['o1', 'o2', 'o3']

result = []
for perm in permutations(TeamA):
    result.append(list(zip(perm, TeamB)))

for res in result:
    print(res)

notice that result is a list of lists, where each one is a list of combinations like you asked

The output of this code would be:

[('p1', 'o1'), ('p2', 'o2'), ('p3', 'o3')]
[('p1', 'o1'), ('p3', 'o2'), ('p2', 'o3')]
[('p2', 'o1'), ('p1', 'o2'), ('p3', 'o3')]
[('p2', 'o1'), ('p3', 'o2'), ('p1', 'o3')]
[('p3', 'o1'), ('p1', 'o2'), ('p2', 'o3')]
[('p3', 'o1'), ('p2', 'o2'), ('p1', 'o3')]

Upvotes: 3

Bart Barnard
Bart Barnard

Reputation: 1168

Something along the following should work:

for a in ['a1','a2','a3']:
    for b in ['b1','b2','b3']:
        res.append( [a,b] )

Upvotes: -1

Related Questions