python - 有时运行有时会出现索引错误
问题描述
我想制作一个随机模拟冠军联赛(足球/足球锦标赛)流程的程序。比赛形式如下:
- 每年有 32 支球队晋级。
- 然后将这些团队随机分为 8 个组,每组 4 个团队。
- 两支球队互相比赛,表现最好的两支球队晋级下一轮(十六强)
- 比赛由一次随机选择 2 支球队来决定,限制条件是同一组的两支球队不得再次相遇
- 然后获胜者进入下一阶段,依此类推,直到我们有一个获胜者。
在我的程序中,我尝试使用 python 中的 random 模块来实现这种格式。但是,每进行 4-5 次迭代,我在第 16 轮选择步骤或四分之一决赛选择步骤中都会出现索引错误。我似乎无法理解为什么。请帮忙
import random
teams = ["Barcelona", "Atlético Madrid", "Real Madrid", "Valencia", "Manchester City", "Liverpool", "Chelsea", "Tottenham Hotspur", "Juventus", "Napoli", "Atalanta", "Internazionale Milano", "Bayern München", "Borussia Dortmund", "RB Leipzig", "Bayer Leverkusen", "Paris Saint-Germain", "LOSC Lille", "Lyon", "Zenit", "Lokomotiv Moskva", "Benfica", "Shakhtar Donetsk", "Genk", "Club Brugge", "Galatasaray", "Salzburg", "Slavia Praha", "Ajax", "Olympiacos", "GNK Dinamo", "Crvena zvezda"]
random.shuffle(teams) #shuffling the 32 teams
Group_A = [] #making empty lists of all 8 groups
Group_B = []
Group_C = []
Group_D = []
Group_E = []
Group_F = []
Group_G = []
Group_H = []
for i in range(32): #using for loop to add each of the 32 teams in group lists
if i <=3:
Group_A.append(teams[i])
elif i <=7 and i>3:
Group_B.append(teams[i])
elif i <=11 and i>7:
Group_C.append(teams[i])
elif i <=15 and i>11:
Group_D.append(teams[i])
elif i <=19 and i>15:
Group_E.append(teams[i])
elif i <=23 and i>19:
Group_F.append(teams[i])
elif i <=27 and i>23:
Group_G.append(teams[i])
else:
Group_H.append(teams[i])
GroupDict = [Group_A, Group_B, Group_C, Group_D, Group_E, Group_F, Group_G, Group_H]
#adding all group lists to another list
print("Group A:\n", Group_A) #displaying group stage draws
print("Group B:\n", Group_B)
print("Group C:\n", Group_C)
print("Group D:\n", Group_D)
print("Group E:\n", Group_E)
print("Group F:\n", Group_F)
print("Group G:\n", Group_G)
print("Group H:\n", Group_H)
r16 = [] #making empty list for the round of sixteen
for i in range(8): #randomly selecting 2 winners from each group and adding
list = GroupDict[i] #and adding them to r16 list 2 at a time
n = 2
r16.append(random.sample(list, n))
print("\n\nTeams that qualified to the round of 16:",r16)
r16_matches = [] #making an empty list to store randomised matchups
for i in range(16): #code to select 2 random teams with
t = random.choice(r16) #the constraint that no two teams from
if not t: #the same group may face each other again
continue
else:
t1 = random.choice(t)
t.remove(t1)
while (1==1):
s = random.choice(r16)
if not s:
continue
else:
if r16.index(s) != r16.index(t):
t2 = random.choice(s)
s.remove(t2)
break
else:
continue
match = [t1,t2]
r16_matches.append(match)
print("\n\nRound of 16 matchups:", r16_matches, "\n\n")
Qfinals = []
Even = [0,2,4,6]
for j in Even: #selecting one winner from each round of sixteen matchup and putting them together, 2 at a time
qt1 = random.choice(r16_matches[j])
qt2 = random.choice(r16_matches[j+1])
qm = [qt1, qt2]
Qfinals.append(qm)
for l in range(4):
print("Quarter final ",l+1," : ",Qfinals[l][0], " vs ",Qfinals[l][1])
Sfinals = []
for k in range(4): #selecting a team from each quarter final matchup to face in semifinals, 2 at a time
S = random.choice(Qfinals[k])
Sfinals.append(S)
print("\n\nSemi Final teams:", Sfinals)
ft1l = [Sfinals[0], Sfinals[1]]
ft2l = [Sfinals[2], Sfinals[3]]
finals = [random.choice(ft1l), random.choice(ft2l)] #selecting random winner from each semi final
print("\n\nFinals: ", finals) #printing finals
print("\n\nWinner: ", random.choice(finals)) #randomly selecting winner from final
解决方案
到达线路时:
Qfinals = []
Even = [0,2,4,6]
for j in Even: #selecting one winner from each round of sixteen matchup and putting them together, 2 at a time
qt1 = random.choice(r16_matches[j])
qt2 = random.choice(r16_matches[j+1])
qm = [qt1, qt2]
Qfinals.append(qm)
有时列表r16_matches
只包含 7 个(索引 0-6)而不是 8 个条目(索引 0-7)。当 j 为 6 时,这会导致r16_matches[j+1]
引发索引错误。
我想,你需要找到这个错误的根源,才能解决你的索引错误。通常在这种情况下,将代码分解成更小的部分会很有帮助,例如通过使用函数。然后,您可以逐个调试函数并验证它们是否完全符合您的期望。
推荐阅读
- javascript - 我可以在更新类组件状态的函数组件中触发 onclick 事件吗?
- php - 如何在 php 中将 unicode 字符从“孤立”的上下文形式更改为通用 Unicode 阿拉伯语
- api - Paperplane html to pdf生成器从本地html页面生成pdf
- python - 有没有更优雅的方法在异常和修复后重新运行函数?
- python - 如何以 Python 方式修复长度为 0 的 flv 文件
- php - 将第一个索引值移动为数组键
- hash - ZipFile 类存档和 Windows 存档之间的不同哈希
- c# - 如何在 Asp.Net MVC 的视图中显示子文件夹中的文件?
- networking - Qemu 网络选项:tap vs bridge
- php - WooCommerce:在申请时检查优惠券元