1

I have a list of lists:

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

It's list of sports teams that made the play offs (i.e NHL or NBA style playoff format, best of 7 rounds). Each list inside the list are pairs of teams that will face off in the first round. There will be 4 rounds to crown a champion.

What I would like to do is remove the team that loses from each pair and re-arrange the larger list so that the winners are re-bracketed (or relisted) in an iterative process so that the next round looks like:

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

and next round:

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

and then

playoffTeamList = [[adSeed1, pdSeed2]]

my idea is to just remove the losing team from each list:

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

I just can't quite figure out how to re-arrange the larger list so that the winning teams remain and are re-bracketed. Zipping or zip unpacking doesn't seem to get me there. Maybe I'm just missing a method or function that allows me to do this.

Also, I am open to other ideas on how to set up my list so it is more elegant in returning the winners from each round and re-arranging for the next round. Maybe a dictionary? Maybe something else?

Mike
  • 4,099
  • 17
  • 61
  • 83
  • 1
    Just as an FYI, it's generally a bad idea to remove things from a sequence you currently iterating over. Read about why [here](http://stackoverflow.com/questions/31704066/floats-not-evaluating-as-negative-python#answer-31704332) – kylieCatt Dec 14 '15 at 18:27

2 Answers2

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

Community
  • 1
  • 1
J0HN
  • 26,063
  • 5
  • 54
  • 85
  • This is great. Thanks @J0HN. I was able to take this and modify it for my use. Thanks for throwing in the print lines too. Makes the print out look nice. – Mike Dec 15 '15 at 03:45
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']]
Kamehameha
  • 5,423
  • 1
  • 23
  • 28
  • Tested and works for my purposes. Thanks a bunch @Kamehameha. I chose J0HN's simply because that was the first one I saw. I'm saving both scripts though. I may change my mind as more of my script develops. – Mike Dec 15 '15 at 03:46