0

I need to create team of 2v2 people

here is my list of player

L=["P1","P2","P3","P4"]

import itertools

I know of to create the all 1v1 or 1v1v1 possible by using

>>> L=["P1","P2","P3","P4"]
>>> for p in itertools.combinations(L,2) :
...     print(p)
... 
('P1', 'P2')
('P1', 'P3')
('P1', 'P4')
('P2', 'P3')
('P2', 'P4')
('P3', 'P4')

or

>>> for p in itertools.combinations(L,3) :
...     print(p)
... 
('P1', 'P2', 'P3')
('P1', 'P2', 'P4')
('P1', 'P3', 'P4')
('P2', 'P3', 'P4')

but how to print all the 2V2 possible ?

Grendel
  • 783
  • 4
  • 12

1 Answers1

2

You could do the following, rather naively but effectively:

for p in itertools.combinations(L,2) :
    o = tuple(x for x in L if x not in p)
    print(p, o)

('P1', 'P2') ('P3', 'P4')
('P1', 'P3') ('P2', 'P4')
('P1', 'P4') ('P2', 'P3')
('P2', 'P3') ('P1', 'P4')
('P2', 'P4') ('P1', 'P3')
('P3', 'P4') ('P1', 'P2')

For more players, you can do:

L = ["P1", "P2", "P3", "P4", "P5"]
for p in itertools.combinations(L,2) :
    o = [x for x in L if x not in p]
    for x in itertools.combinations(o, 2):
        print(p, x)

Which would you give a double round-robin ;) For a single round-robin:

for p in itertools.combinations(L,2) :
    o = [x for x in L if x > p[0] and x != p[1]]
    for x in itertools.combinations(o, 2):
        print(p, x)

('P1', 'P2') ('P3', 'P4')
('P1', 'P2') ('P3', 'P5')
('P1', 'P2') ('P4', 'P5')
('P1', 'P3') ('P2', 'P4')
('P1', 'P3') ('P2', 'P5')
('P1', 'P3') ('P4', 'P5')
('P1', 'P4') ('P2', 'P3')
('P1', 'P4') ('P2', 'P5')
('P1', 'P4') ('P3', 'P5')
('P1', 'P5') ('P2', 'P3')
('P1', 'P5') ('P2', 'P4')
('P1', 'P5') ('P3', 'P4')
('P2', 'P3') ('P4', 'P5')
('P2', 'P4') ('P3', 'P5')
('P2', 'P5') ('P3', 'P4')
user2390182
  • 72,016
  • 6
  • 67
  • 89