简体   繁体   English

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

[英]How to split an array in a for loop Python

I have the following array: 我有以下数组:

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

I am looping over images in a test dataset, and what I want is for each image, to get the corresponding prediction. 我正在遍历测试数据集中的图像,我想要的是每个图像,以获得相应的预测。 So for example, for image1 the prediction would be 0, for image2 it would be 1 etc. 因此,例如,对于image1,预测将为0,对于image2,预测将为1,依此类推。

I've been trying something like this, I know it's wrong but it gives you an idea of what I want: 我一直在尝试这样的事情,我知道这是错误的,但是它可以使您了解我想要的东西:

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)

(I've omitted the part of the code that isn't relevant, but if you need to see it i'll edit the post and add it in) (我省略了与代码无关的部分,但是如果您需要查看它,我将编辑该帖子并将其添加进来)

Is there a way to do what I'm asking? 有什么方法可以满足我的要求吗?

Thanks in advance! 提前致谢!

You can do: 你可以做:

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

This works for any iterable: 这适用于任何迭代:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM