在python中如何返回元组的元组?i、 (1,8,(4,5)),(2,7,(3,6)),它一直不返回任何值

2024-05-26 21:52:39 发布

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

我正在研究Python 3中的递归算法,该算法生成一个包含元组的元组列表。当我试图返回列表时,没有得到任何结果。有人能发现我遗漏了什么吗? 以下是我所拥有的:

def plan_nba_playoff(num_teams):
    games =list(range(1, num_teams+1))
    return plan_nba_helper(games)

def plan_nba_helper(teams):
    newGames = []
    c = len(teams) - 1
    for i in range(len(teams)//2):
        game = teams[i], teams[c],
        newGames.append(game)
        c -= 1
        if i == c and len(newGames) == 1:
            print(newGames)
            return newGames
        elif i == c:
            plan_nba_helper(newGames) #((((1, 8), (4, 5)), ((2, 7), (3, 6))),)

print(plan_nba_playoff(8)) #((((1, 8), (4, 5)), ((2, 7), (3, 6))),)

Tags: helper算法列表lenreturndefrangenum

热门问题