【问题标题】:list index out of range (notcars[5])?列表索引超出范围(notcars[5])?
【发布时间】:2021-10-25 02:16:23
【问题描述】:

可能是一个菜鸟问题,但在运行时使此错误列表索引超出范围。

images = glob.glob('/OwnCollection')

cars = []
notcars = []
all_cars = []
all_notcars = []

for image in images:
    if 'non-vehicles' in image:
        all_notcars.append(image)
    else:
        all_cars.append(image)

# Get only 1/5 of the training data to avoid overfitting
for ix, notcar in enumerate(all_notcars):
    if ix % 5 == 0:
        notcars.append(notcar)
        
for ix, car in enumerate(all_cars):
    if ix % 5 == 0:
        cars.append(car)
        
car_image = mpimg.imread(cars[1])
notcar_image = mpimg.imread(notcars[5])

错误代码

---> 24 car_image = mpimg.imread(cars[1])
     25 notcar_image = mpimg.imread(notcars[5])
     26 

IndexError: list index out of range

文件夹内 有两个文件夹名为“non-vehicles”,第二个文件夹名为“vehicles”

非常感谢任何帮助。

【问题讨论】:

    标签: python-3.x tensorflow logic


    【解决方案1】:

    您的代码命令像这样获取编号为 1 和 5 的数组

    car_image = mpimg.imread(cars[1])
    notcar_image = mpimg.imread(notcars[5])
    

    但是在这个程序中有一个小于1的数组在汽车中,所以你必须知道你的数组用命令有多长

    print(len(cars))
    

    而且你的数组命令小于长数组

    【讨论】:

      【解决方案2】:

      如果您访问 Python 列表中的无效索引,则会出现“列表索引超出范围”错误。换句话说,如果您保存了一个变量,它将存储在数组的第一个单元格中

      所以你的代码可能需要像这样

      car_image = mpimg.imread(cars[0])
      

      也许仔细检查你的图像在哪里可能有用,如果找不到任何东西,这可能会导致索引错误

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-12-16
        • 2011-06-14
        • 2016-06-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多