生成第 5、7、9 等位置的匹配项

Generating matches for 5th, 7th, 9th etc. Places

我有一种方法可以为小组赛阶段的两支队伍中的每支队伍生成比赛。

Group A
T1───┐
     │
T2───┘
     ├───┐
T3───┐   │   ├───T1
     │   │   │
T4───┘   │   ├───T6
Group B  ├───│
T5───┐   │   ├───T2
     │   │   │
T6───┘   │   ├───T5
     ├───┘
T7───┐
     │
T8───┘

def generate_final_stage(advanced_teams):
    teams_from_group_stage = advanced_teams
    matches = []
    for i in range(len(teams_from_group_stage)):
        teams = []
        if i != len(teams_from_group_stage) - 1:
            team_1 = teams_from_group_stage[i][0]
            team_2 = teams_from_group_stage[i + 1][1]
            teams.append(team_1)
            teams.append(team_2)
        else:
            team_1 = teams_from_group_stage[i][0]
            team_2 = teams_from_group_stage[0][1]
            teams.append(team_1)
            teams.append(team_2)
        matches.append([teams[0], teams[1]])
    return matches


def main():
    # Possible Inputs
    advanced_teams = [[1, 2, 3], [4, 5, 6]]
    advanced_teams2 = [[1, 2, 3, 4], [5, 6, 7, 8]]
    advanced_teams3 = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]
    schedule = generate_final_stage(advanced_teams3)
    print(schedule)


if __name__ == "__main__":
    main()

我想改进这个脚本,以便它也能为后续地点生成匹配项。如果有 3、4、5 或更多的球队进入季后赛,这个脚本应该相应地生成比赛。如果小组赛阶段的球队数量不是奇数,则必须按如下方式生成比赛:最佳球队对最差球队。例如:

1st. Team from Group A vs. 2nd Team from Group B
1st. Team from Group B vs. 2nd Team from Group A
when 3 teams go from the group stage to the PlayOff.
3rd. Team from Group A vs. 3rd Team from Group B
when 4 teams go from the group stage to the PlayOff.
3rd. Team from Group A vs. 4th Team from Group B
3st. Team from Group B vs. 4th Team from Group A
when 5 teams go from the group stage to the PlayOff.
5th. Team from Group A vs. 5th Team from Group B
and so on

例如来自 [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]] 的输入,我期望以下输出:

[[1, 7], [6, 2], [3, 9], [4, 8], [5, 10]]

组数也是动态的,可以有2、4、6、8、10等组。在这种情况下,前两组互相玩,接下来的两个相同,下一个和下一个 [[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, 18], [19, 20, 21, 22, 23, 24]] 输出应该是:

[(1, 8), (7, 2), (3, 10), (9, 4), (5, 12), (11, 6), (13, 20), (14, 19), (15, 22), (16, 21), (17, 24), (18, 23)]

N-teams¹ in M-groups²:

的情况下,您可以使用递归来获得所需的结果
def playoff2g(g1, g2, r):
    """ Get matches for 2 groups """
    if len(g1) > 1:
        r.extend([(g1[0], g2[1]), (g2[0], g1[1])])
        playoff2g(g1[2:], g2[2:], r)
    elif len(g1) == 1:
        r.append((g1[0], g2[0]))
    return r

def playoff(gs):
    """ Get matches for multiple number of groups """
    res = []
    for i in range(0, len(gs)-1, 2):
        res = playoff2g(gs[i], gs[i+1], res)
    return res

groups = [[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, 18], [19, 20, 21, 22, 23, 24]]
result = playoff(groups)

输出:

[(1, 8), (7, 2), (3, 10), (9, 4), (5, 12), (11, 6), (13, 20), (19, 14), (15, 22), (21, 16), (17, 24), (23, 18)]

¹ 所有小组的队伍数量应该相同

² 组数应该是偶数 (2,4,6...)