我想在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"]
有什么想法吗?
答案 0 :(得分:10)
夹具调度是一个众所周知的问题。这是在http://en.wikipedia.org/wiki/Round-robin_tournament
中给出的算法的python实现# 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')]
答案 1 :(得分:4)
我想评论来自@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]))
答案 2 :(得分:1)
@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')]