# sharing/swapping items between lists

samwyse samwyse at gmail.com
Wed Apr 15 13:57:58 CEST 2009

```On Apr 14, 7:01 pm, Aaron Brady <castiro... at gmail.com> wrote:
> Here is an idea.  Create a list of all possible pairs, using
> itertools.combinations.  You'll notice everyone gets equal play time
> and equal time against each other on a pair-by-pair basis.  Then, call
> random.shuffle until one player isn't playing on two courts in one
> day.
>
> It has the disadvantage that you might end up with player A playing
> lots early on and rarely at the end, and B rarely early on and lots at
> the end.  Perhaps you could generate a few to several correct
> solutions, then choose the most evenly distributed.  Does this make
> sense?

Here's my idea:  generate all possible pairs:
>>> import itertools
>>> players = [chr(c) for c in xrange(ord('a'),ord('z')+1)]
>>> all_pairs = list(itertools.combinations(players,2))

partition the list:
>>> def choose_nonoverlapping(pairs):
chosen, leftover, used = list(), list(), list()
for p in pairs:
a, b = p
if a in used or b in used:
leftover.append(p)
else:
chosen.append(p)
used.append(a)
used.append(b)
return chosen, leftover

>>> court_count = 10
>>> week_count = 10
>>> pairs = all_pairs
>>> for week in xrange(week_count):
print 'week', week+1
this_week, pairs = choose_nonoverlapping(pairs)
print ', '.join(map(lambda t: ' vs '.join(t), this_week
[:court_count]))
pairs[0:0] = this_week[court_count:]

week 1
a vs b, c vs d, e vs f, g vs h, i vs j, k vs l, m vs n, o vs p, q vs
r, s vs t
week 2
u vs v, w vs x, y vs z, a vs c, b vs d, e vs g, f vs h, i vs k, j vs
l, m vs o
week 3
n vs p, q vs s, r vs t, a vs d, b vs c, e vs h, f vs g, i vs l, j vs
k, m vs u
week 4
o vs v, w vs y, x vs z, a vs e, b vs f, c vs g, d vs h, i vs m, j vs
n, k vs p
week 5
l vs q, r vs s, t vs u, a vs f, b vs e, c vs h, d vs g, i vs n, j vs
m, k vs o
week 6
p vs v, w vs z, x vs y, a vs g, b vs h, c vs e, d vs f, i vs o, j vs
q, k vs m
week 7
l vs n, r vs u, a vs h, b vs g, c vs f, d vs e, i vs p, j vs o, k vs
q, m vs s
week 8
t vs v, a vs i, b vs j, c vs k, d vs l, e vs m, f vs n, g vs o, h vs
p, q vs u
week 9
r vs w, s vs x, a vs j, b vs i, c vs l, d vs k, e vs n, f vs m, g vs
p, h vs o
week 10
q vs t, u vs y, v vs z, a vs k, b vs l, c vs i, d vs j, e vs o, f vs
p, g vs m

```