使用Python消除季后赛括号中的球队

时间:2015-12-14 17:45:39

标签: python

我有一份清单清单:

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

它的运动队名单(即NHL或NBA风格的季后赛形式,最好的7轮)。列表中的每个列表都是在第一轮中将面对的一对团队。将有4轮冠军冠军。

我想要做的是删除每对丢失的团队并重新安排更大的列表,以便在迭代过程中重新包围(或重新)括号,以便下一轮看起来像: / p>

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

我只是无法弄清楚如何重新安排更大的名单,以便获胜的队伍保持并重新加入方括号。拉链或拉链拆包似乎没有让我到那里。也许我只是错过了允许我这样做的方法或功能。

此外,我对如何设置我的列表的其他想法持开放态度,因此从每轮回归获胜者并重新安排下一轮更为优雅。也许是字典?也许别的什么?

2 个答案:

答案 0 :(得分:2)

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)

The magic lies in plan_games function - zip takes two iterables and joins them element-wise, in order. [::2] and [1::2] are list slicings - well explained in other SO question

答案 1 :(得分:2)

A simple way is to loop through them and create a new list at the end of each round.

# 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

Here is a working example -

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

Running this - (Each time you run this, it'll yield a different result since I've randomized the playoffScoring function)

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