# sharing/swapping items between lists

Ross ross.jett at gmail.com
Tue Apr 14 19:34:07 CEST 2009

```On Apr 14, 5:57 am, a... at pythoncraft.com (Aahz) wrote:
>
>
>
> Ross  <ross.j... at gmail.com> wrote:
> >On Apr 13, 9:08=A0am, a... at pythoncraft.com (Aahz) wrote:
> >> In article <c569228f-f391-4317-83a2-08621c601... at r8g2000yql.googlegroups.=
> >com>,
> >> Ross =A0<ross.j... at gmail.com> wrote:
>
> >>>I'm sorry...my example was probably a bad one. A better example of
> >>>output I would like would be something like [[1,2],[3,4],[5,6]] and
> >>>then for the leftovers list [7,8,9,10 etc]. What I'm trying to do is
> >>>produce some sort of round robin algorithm for tennis that is
> >>>constrained by the number of courts available each week. So if there
> >>>are only 3 courts available for a singles league and 10 people have
> >>>signed up, 4 players will have a bye each week. I want my algorithm to
> >>>produce unique matchups each week and also give each player the same
> >>>angle?
>
> >> How about Googling for "round robin algorithm python"? ;-)
>
> >I have the basic algorithm and it works fine...I'm just having trouble
> >adding another parameter to it that allows for court constraints and
> >bye weeks.
>
> the core algorithm you're using?
> --
> Aahz (a... at pythoncraft.com)           <*>        http://www.pythoncraft.com/
>
> Why is this newsgroup different from all other newsgroups?

Here's the core algorithm I'm using:

>>> def round_robin(teams,rounds):
if len(teams)%2:
teams.append(None)
mid = len(teams) //2
for i in range(rounds):
yield zip(teams[:mid], teams[mid:])
teams = teams[0:1] + teams[mid:mid+1] + teams[1:mid-1]+teams[mid
+1:]+teams[mid-1:mid]

>>> if __name__== '__main__':
rounds = 15
teams = range(16)
for round in round_robin(teams,rounds):
print round

```