如何在 for 循环中拆分数组 Python

How to split an array in a for loop Python

我有以下数组:

prediction = [0 1 0 0 0 1 0 0 0 0 0 1 1 1 1 1 0 0 1 0]

我正在遍历测试数据集中的图像,我想要的是对每张图像进行相应的预测。因此,例如,对于 image1,预测将为 0,对于 image2,它将为 1 等

我一直在尝试这样的事情,我知道这是错误的,但它让您了解我想要什么:

clf = svm.SVC(kernel='linear', C=100)

for file in glob.glob(test_path + "/*.jpg"):
.
.
.
    prediction = clf.predict(X_test)

    for i in prediction:
        prediction = prediction[i]
        print(prediction)

(我省略了不相关的代码部分,但如果您需要查看它,我会编辑 post 并将其添加进去)

有什么方法可以满足我的要求吗?

提前致谢!

你可以这样做:

for index,file in enumerate(glob.glob(test_path + "/*.jpg")):
    prediction[index] #your prediction for the indexth image

这适用于任何可迭代对象:

for i, each in enumerate(iterable):
    print('The'+str(i)+'th element in your iterable is'+str(each))