lis中无序对值的Python数据结构

2024-10-16 20:48:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我可能没有使用正确的关键字来找到一些有用的东西(你可能可以从这个尴尬的标题中看出这一点),所以这里是:

如何表示单个列表中成对的值?例如,这个列表可以是一个团队联盟(比如说红色金色蓝色以保持简短)相互竞争,并且每个游戏的结果值。每个列表项(团队)与所有其他项配对,并且每个配对对应一个值,我们可以将其称为结果。通常使用这种结构的方法是挑选两支球队来查看比赛结果。双人不分先后顺序,团队也没有特殊状态。你知道吗

最明显的是一个表(例如,我使用的是Pandas,所以是dataframe),但感觉有点尴尬,因为如果我们要避免重复,它必须是半满的。你知道吗

enter image description here

“团队”之间可能有两个“游戏”(这在体育运动中很常见),但在这种情况下,我认为表格会更糟,因为我们必须给对角线上方和下方的部分赋予一些含义(比如“第1轮”和“第2轮”),而且它不能概括为更多的匹配(应该在“单个”列表的顶部添加一个额外的变量)。你知道吗

enter image description here

什么是“自然的”(代替更好的术语)来表示这一点?(通过这个,我指的是原始表,即来自单个列表的无序对

p.S.:我提到了数据帧,但这个问题适用于任何类型的“表”(例如dict of dicts)。你知道吗

p.S.2:我提到“团队”只是为了说明。请避免讨论与运动有关的问题!你知道吗


Tags: 方法游戏标题pandas列表状态关键字团队
2条回答

我建议用一本字典,可能是

例如

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

也就是说

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}}

我觉得做东西比较好。你知道吗

在python文件中:

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")

如果执行该文件,它将输出:

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

编辑:班级联盟

# 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")

它输出:

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

这能回答你的问题吗?你知道吗

如果您想像第一个问题中那样显示一个更直观的表,那么制作一个GUI应该更有效。你知道吗

相关问题 更多 >