这是我的代码,当我尝试运行它时,它显示以下错误。
“列表索引超出范围”。
请帮助我弄清楚并解决此错误。还可以帮助我了解为什么会这样吗?
t = int(input())
j = list(map(int,input().split()))
d=list(map(int,input().split()))
a =[]
b=[]
c=[]
for i in range(len(j)):
l,h,m=a[0],b[0],c[0]
if d[i]==1:
a.append(j[i])
if d[i]==2:
b.append(j[i])
if d[i]==3:
c.append(j[i])
a.sort()
b.sort()
c.sort()
if l+h>m:
print(m)
if l+h<m:
print(l+h)
最佳答案
列表索引超出范围每当您尝试调用不可用的索引时,就会发生。在您的代码列表中,a,b,c为空列表,但您尝试调用第一个元素(a [0],b [0],c [0])
关于python - 简单代码中的列表索引超出范围错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49829627/