5

我想用 python 写一个 League Fixture 生成器,但我做不到。这是详细信息:

有一个动态的团队列表,例如teams = ["Team1", "Team2", "Team3", "Team4"]. 如何从团队列表中生成 fixture_weekx 列表?例如:

fixture_week1 = ["Team1", "Team2", "Team3", "Team4"]
fixture_week2 = ["Team1", "Team3", "Team2", "Team4"]
fixture_week2 = ["Team1", "Team4", "Team2", "Team3"]

#Return matches:
fixture_week1 = ["Team2", "Team1", "Team4", "Team3"]
fixture_week2 = ["Team3", "Team1", "Team4", "Team2"]
fixture_week2 = ["Team4", "Team1", "Team3", "Team2"]

任何的想法?

4

3 回答 3

9

夹具调度是一个众所周知的问题。这是在以下位置给出的算法的 python 实现:http ://en.wikipedia.org/wiki/Round-robin_tournament

# generation code - for cut and paste

import operator
def fixtures(teams):
    if len(teams) % 2:
        teams.append('Day off')  # if team number is odd - use 'day off' as fake team     

    rotation = list(teams)       # copy the list

    fixtures = []
    for i in range(0, len(teams)-1):
        fixtures.append(rotation)
        rotation = [rotation[0]] + [rotation[-1]] + rotation[1:-1]

    return fixtures

# demo code
teams = ["Team1", "Team2", "Team3", "Team4", "Team5"]

# for one match each - use this block only
matches = fixtures(teams)
for f in matches:    
    print zip(*[iter(f)]*2)

# if you want return matches 
reverse_teams =  [list(x) for x in zip(teams[1::2], teams[::2])]
reverse_teams = reduce(operator.add,  reverse_teams)    # swap team1 with team2, and so on ....

#then run the fixtures again
matches = fixtures(reverse_teams)

print "return matches"
for f in matches:    
    print f

这会生成输出:

[('Team1', 'Day off'), ('Team2', 'Team5'), ('Team3', 'Team4')]
[('Team1', 'Team5'), ('Day off', 'Team4'), ('Team2', 'Team3')]
[('Team1', 'Team4'), ('Team5', 'Team3'), ('Day off', 'Team2')]
[('Team1', 'Team3'), ('Team4', 'Team2'), ('Team5', 'Day off')]
[('Team1', 'Team2'), ('Team3', 'Day off'), ('Team4', 'Team5')]
于 2012-06-28T13:57:18.597 回答
6

我想评论一下@MariaZverina 的代码不太好用。我按原样尝试了,但我没有得到正确的配对。我在下面所做的修改适用于她的代码。不同之处在于,我通过将夹具 f 的前半部分与相反的后半部分拉上拉链来对每个夹具进行彩虹式配对。

# demo code
teams = ["Team1", "Team2", "Team3", "Team4", "Team5"]

# for one match each - use this block only
matches = fixtures(teams)    
for f in matches:    
    # This is where the difference is.
    # I implemented "rainbow" style pairing from each fixture f 
    # In other words: 
    # [(f[0],[f[n-1]), (f[1],f[n-2]), ..., (f[n/2-1],f[n/2])], 
    # where n is the length of f
    n = len(f)
    print zip(f[0:n/2],reversed(f[n/2:n]))
于 2016-10-20T18:29:45.340 回答
4

@MariaZverina 的代码不起作用,我也使用 Round-robin_tournament 实现了此代码:

teams = ["Team1", "Team2", "Team3", "Team4", "Team5", "Team6"]
if len(teams) % 2:
    teams.append('Day off')
n = len(teams)
matchs = []
fixtures = []
return_matchs = []
for fixture in range(1, n):
    for i in range(n/2):
        matchs.append((teams[i], teams[n - 1 - i]))
        return_matchs.append((teams[n - 1 - i], teams[i]))
    teams.insert(1, teams.pop())
    fixtures.insert(len(fixtures)/2, matchs)
    fixtures.append(return_matchs)
    matchs = []
    return_matchs = []

for fixture in fixtures:
    print fixture

输出:

[('Team1', 'Team6'), ('Team2', 'Team5'), ('Team3', 'Team4')]
[('Team1', 'Team5'), ('Team6', 'Team4'), ('Team2', 'Team3')]
[('Team1', 'Team4'), ('Team5', 'Team3'), ('Team6', 'Team2')]
[('Team1', 'Team3'), ('Team4', 'Team2'), ('Team5', 'Team6')]
[('Team1', 'Team2'), ('Team3', 'Team6'), ('Team4', 'Team5')]
[('Team6', 'Team1'), ('Team5', 'Team2'), ('Team4', 'Team3')]
[('Team5', 'Team1'), ('Team4', 'Team6'), ('Team3', 'Team2')]
[('Team4', 'Team1'), ('Team3', 'Team5'), ('Team2', 'Team6')]
[('Team3', 'Team1'), ('Team2', 'Team4'), ('Team6', 'Team5')]
[('Team2', 'Team1'), ('Team6', 'Team3'), ('Team5', 'Team4')]
于 2017-12-31T02:02:32.687 回答