0

I'm probably not using the right keywords to find something useful on this (as you might be able to tell from the awkward title), so here it goes:

How could I represent values for pairs from a single list? For example, the list could be a league of teams (say Red, Gold and Blue to keep it short) playing against each other and the values the result of each game. Each list item (team) is paired with all others, and each pairing corresponds to a value, which we can call the result. And the normal way one would use this structure would be to pick two teams to look up the result of the match. Pairs are not ordered and teams have no special status.

The most obvious thing would be a table (e.g., I'm using Pandas, so a dataframe), but it feels a bit awkward because it would have to be half-full if we're avoiding repetition.

enter image description here

The "teams" could possibly have two "games" between them (which is common in sports, anyway), but in this case, I think a table would be even worse because we'd have to assign some meaning to the parts above and below the diagonal (say, "round 1" and "round 2"), and it's not generalizable to even more matches (that should be done with an additional variable on top of the "single" list).

enter image description here

What's a "natural" (in lieu of a better term) way to represent this? (By this, I mean the original table, i.e., unordered pairs from a single list.)

P.S.: I mentioned dataframes, but the question would apply to any type of "table" (e.g., dict of dicts).

P.S.2: I mentioned "teams" for illustration purposes only. Please avoid discussions of sport-related issues!

1
  • given a list l = ['red', 'blue', 'green'], are you looking for something like this [('red', 'blue'), ('red', 'green'), ('blue', 'green')]? Commented Mar 1, 2019 at 9:40

2 Answers 2

1

I think it's better to make objects.

In a python file:

class Team():
    def __init__(self, name):
        self.name = name

    def __str__(self):
        return str(self.name)

class Game():
    def __init__(self, team_1, team_2, result):
        self.team_1 = team_1
        self.team_2 = team_2
        self.result = result

    def __str__(self):
        return f"{self.team_1} vs {self.team_2}: {self.result}"


# some tests
if __name__ == "__main__":
    red, gold, blue = Team("red"), Team("gold"), Team("blue")
    red_blue = Game(red, blue, "v1")
    red_gold = Game(red, gold, "v2")
    blue_gold = Game(blue, gold, "v3")
    print(red_blue, red_gold, blue_gold, sep="\n")

If you execute the file, it outputs:

red vs blue: v1
red vs gold: v2
blue vs gold: v3

EDIT: class League

# add this code in the same module that the previous one 

class League():
    def __init__(self, name):
        self.name = name
        self.teams = []
        self.games = []

    def add_teams(self, *teams):
        # *teams is a sequence of Team instances
        for team in teams:
            self.teams.append(team)

    def add_games(self, *games):
        # *games is a sequence of Game instances
        for game in games:
            self.games.append(game)

    def get_game_by_team(self, team):
        print(f"Results for all games of {team} in {self.name} league:")
        for game in self.games:
            if team == game.team_1.name or team == game.team_2.name:
                print(game)

if __name__ == "__main__":
    league = League("master")
    league.add_teams(red, gold, blue)
    league.add_games(red_blue, red_gold, blue_gold)
    league.get_game_by_team("red")
    league.get_game_by_team("blue")
    league.get_game_by_team("gold")

It outputs:

Results for all games of red in master league:
red vs blue: v1
red vs gold: v2
Results for all games of blue in master league:
red vs blue: v1
blue vs gold: v3
Results for all games of gold in master league:
red vs gold: v2
blue vs gold: v3

Is that answer your question ?

If you want to display a more visual table like you show in your first question, it should be more efficient to try to make a GUI.

Sign up to request clarification or add additional context in comments.

2 Comments

True, there's always the option of a custom class. I was just hoping there was a simpler way to just get unordered pairs from the ordered pairs of a table!
As this might be the way to go at the end, are you able to expand on what the "League" (Table) class would look like? In particular, how would you get usual table features for accessing elements (e.g., if my_team is in this_league: or for each team in this_league)? This is a bit of a different question, so feel free to simply point to a more general answer/guide on how to do these things, but either way, I think it would make sense to be included in this answer.
0

I'd suggest to use a dictionary of dictionary may be,

For example,

{'red':{'gold':'v1','blue':'v2'},'gold':{'red':'v4','blue':'v3'},'blue':{'red':'v5','gold':'v6'}}

That is,

match = 0
>>> def get_next_match():
...     global match
...     match+=1
...     return match
...
>>> {team:{t:get_next_match() for t in teams if t!=team} for team in teams}
{'blue': {'gold': 6, 'red': 5}, 'gold': {'blue': 4, 'red': 3}, 'red': {'blue': 2, 'gold': 1}}

1 Comment

Thanks for the reply, but as I've mentioned in the question, this is not what I'm looking for. With your dictionary, yourdict['blue']['gold'] is different to and independent of yourdict['gold']['blue'].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.