The football tournament is a team in a season to face the league in addition to their own team in the game, for which we can use a loop algorithm to achieve, then take a look at the Python Design Football League schedule program and simple implementation of the idea of the example:
Every year the Serie A Bundesliga tournament is a must-see for the fans, remembering the code that was written to build the schedule, and it's too comfortable to use Python to write this kind of stuff.
This algorithm is called the Snake Ring algorithm.
That is, put all the teams into a ring (2 columns), the left side against the right, the first team does not move, the other teams clockwise circulation, so it certainly does not repeat.
For illustrative purposes, suppose there are 8 teams a to H. Press the ring line as below.
In this way, the 1th round of the game is, (a,h) (b,g) (c,f) (d,e).
The next round, the first team A does not move, the other teams like gears clockwise to walk a grid.
In this way, the 2nd round of the game is, (A, B) (c,h) (d,g) (e,f).
The gears continue to slide until they return to their original point, so that each team is playing against all 7 other teams.
Thousand words is inferior to one yard. Take the Premier League as an example.
From collections Import deque import random def build_schedule (_teamarr): Scheduleobj = Dict.fromkeys (range (1,20)) fix pos = _teamarr[0] ring = _teamarr[1:] ring = deque (ring) #前半赛程, 1-19-wheel (round) for round in range (1,20): #第1支球队不动 , plus rotation (rotate) of the ring teams = [Fixpos] + list (ring) #切成2列, left side home, right side, away = Teams[:len (teams)/2],teams[len (tea MS)/2:] away = Away[::-1] #随机打乱主客队 Scheduleobj[round] = [(x, Y) if Random.random () >=0.5 Else (y,x) for x, y i n Zip (Home,away)] Ring.rotate (1) #后半赛季对阵跟前半赛季一样, but the home and away is swapped for round in range (20,39): scheduleobj[round] = [(y,x) For x, y in scheduleobj[round-19]] return scheduleobj if __name__ = = ' __main__ ': Teamarr = [u ' Manchester United ', U ' Aston Villa ', U ' Chelsea ', u ' West Ham ', U ' Fulham ', U ' Spurs ', U ' Liverpool ', U ' Southampton ', U ' Everton ', U ' Norwich ', U ' newcastle ', U ' Manchester City ', U ' Stoke ', U ' Sunderland ', U ' Crystal Palace ', U ' West Bromwich Albion ', U ' Arsenal ', U ' Hull City ', U ' Cardiff City ', U ' Swansea '] scheduleobj = Build_schedule (Teamarr) print U '---League 1th round---' for h,a in Sche DULEOBJ[1]: Print u '{}-{} '. Format (h,a) print U '---League 2nd round---' for h,a in scheduleobj[2]: print u ' {}-{} '. Format (h,a)