【问题标题】:Eliminating Teams in Playoff Brackets Using Python使用 Python 在季后赛中淘汰球队
【发布时间】:2016-03-20 07:24:06
【问题描述】:

我有一个列表列表:

playoffTeamList = [[adSeed1, adSeed4], [adSeed2, adSeed3], 
                   [mdSeed1, mdSeed4], (mdSeed2, mdSeed3], 
                   [cdSeed1, cdSeed4], (cdSeed2, cdSeed3], 
                   [pdSeed1, pdSeed4], (pdSeed2, pdSeed3]]

这是进入季后赛的运动队列表(即 NHL 或 NBA 风格的季后赛格式,7 轮最佳)。列表中的每个列表都是将在第一轮对决的团队对。将有4轮加冕冠军。

我想做的是从每对中删除失败的团队并重新排列更大的列表,以便在迭代过程中将获胜者重新放入(或重新列出),以便下一轮看起来像:

playoffTeamList = [[adSeed1, adSeed2], 
                   [mdSeed1, mdSeed2], 
                   [cdSeed1, cdSeed2], 
                   [pdSeed1, pdSeed2]]

下一轮:

playoffTeamList = [[adSeed1, mdSeed1], 
                   [cdSeed1, pdSeed2]] 

然后

playoffTeamList = [[adSeed1, pdSeed2]]

我的想法是从每个列表中删除失败的团队:

for brackets in playoffTeamList:
    playoffScoring() # This is an algorithm that plays each game of each best of seven round and returns a winner and/or loser
    brackets.remove(brackets[0])

print playoffTeamList

我只是想不通如何重新排列更大的列表,以便获胜的团队保留并重新加入括号。压缩或解压缩似乎并没有让我到达那里。也许我只是缺少一个允许我这样做的方法或函数。

此外,我对如何设置我的列表的其他想法持开放态度,这样在返回每一轮的获胜者并重新安排下一轮时会更加优雅。也许是字典?也许还有别的?

【问题讨论】:

  • 仅供参考,从当前迭代的序列中删除内容通常是个坏主意。了解为什么here

标签: python


【解决方案1】:
def playoffScoring(team1, team2):
    return team1  # replace with actual scoring function

def games_round(games):
    winners = []
    for team1, team2 in games:
        winning_team = playoffScoring(team1, team2) 
        winners.append(winning_team)

    return winners

def plan_games(teams):
    return zip(teams[::2], teams[1::2])

teams = [1, 2, 3, 4, 5, 6, 7, 8]
round = 0
while len(teams) > 1:
     round += 1
     print "Round {}: teams: {}".format(round, teams)
     games = plan_games(teams)
     teams = games_round(games)

champion = teams[0]  # only one left
print "Champion is {}".format(champion)

神奇之处在于 plan_games 函数 - zip 采用两个可迭代对象并按元素顺序将它们连接起来。 [::2][1::2] 是列表切片 - 在其他 SO question 中有很好的解释

【讨论】:

  • 这很棒。谢谢@J0HN。我能够接受并修改它以供我使用。感谢您也加入打印行。使打印出来的效果很好。
【解决方案2】:

一种简单的方法是遍历它们并在每轮结束时创建一个新列表。

# Iterate through each round
for round in xrange(num_rounds):
    winners = []

    # Make a list of only the winners in each round
    for playoff in playoffTeamList:
        # Run your algo
        winner_index = playoffScoring(*playoff)
        winners.append(playoff[winner_index])

    # At the end of each round, aggregate the winners in pairs
    playoffTeamList = [winners[i:i+2] for i in xrange(0, len(winners), 2)]
    print playoffTeamList

这是一个工作示例 -

import math
from random import randint

# Return randomly between 0 and 1(To be replaced by your algo)
def playoffScoring(team1, team2):
    return randint(0, 1)


playoffTeamList = [
    ["T1",  "T2"], ["T3",  "T4"],
    ["T5", "T14"], ["T13", "T12"],
    ["T6", "T15"], ["T3",  "T11"],
    ["T7",  "T8"], ["T9",  "T10"]
]

num_rounds = int(math.log(len(playoffTeamList), 2)) + 1
# num_rounds = 4

for round in xrange(num_rounds):
    winners = []
    for playoff in playoffTeamList:
        winner_index = playoffScoring(*playoff)
        winners.append(playoff[winner_index])
    playoffTeamList = [winners[i:i+2] for i in xrange(0, len(winners), 2)]
    print playoffTeamList

运行这个 - (每次运行它都会产生不同的结果,因为我已经随机化了 playoffScoring 函数)

# OUTPUTS - 
[['T2', 'T3'], ['T5', 'T13'], ['T6', 'T11'], ['T8', 'T9']]
[['T3', 'T13'], ['T11', 'T8']]
[['T13', 'T8']]
[['T13']]

【讨论】:

  • 经过测试并适用于我的目的。非常感谢@Kamehameha。我选择 J0HN 只是因为那是我看到的第一个。不过,我正在保存两个脚本。随着剧本的发展,我可能会改变主意。
猜你喜欢
  • 1970-01-01
  • 2017-08-26
  • 2015-06-11
  • 2012-10-03
  • 1970-01-01
  • 2013-04-10
  • 1970-01-01
  • 2017-02-15
  • 2018-03-31
相关资源
最近更新 更多