python - 简单代码中的列表索引超出范围错误

标签 python compiler-errors runtime-error

这是我的代码,当我尝试运行它时,它显示以下错误。
“列表索引超出范围”。
请帮助我弄清楚并解决此错误。还可以帮助我了解为什么会这样吗?

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/

相关文章:

python - 如何在 Keras 生成器中引用纪元号?

python - 使用 python 更改线程中的对象值

python-3.x - TypeError : Layout of the output array img is incompatible with cv::Mat (step[ndims-1] != elemsize or step[1] != elemsize*nchannels)

android - Google Cloud Messaging错误: Unable to instantiate receiver

Python PEP479 更改生成器内部的 StopIteration 处理

python - 为什么 conda 无法创建安装了 python=3.4 的环境

c# - IIS服务器找不到index.aspx文件-404错误

java - 错误 : not a statement Else(

runtime-error - box2d CreateFixture 和 b2FixtureDef 给出纯虚函数调用

python - PIL : How to reopen an image after verifying?