简体   繁体   English

Python - 如何从不是第 0 个元素而是第 x 个元素循环数组?

[英]Python - how to for loop an array from not 0th but xth element?

I have a for loop like this我有一个像这样的for循环

ImgPaths = make_dataset(TestImgPath)
    for i, ImgPath in enumerate(ImgPaths):

In which:其中:

  • TestImgPath is the path to input images. TestImgPath 是输入图像的路径。 Type string.键入字符串。 In my case TestImgPath = './TestData/TestWhole' .在我的情况下TestImgPath = './TestData/TestWhole' TestData and TestWhole are folders. TestDataTestWhole是文件夹。
  • ImgPath is the path to image. ImgPath 是图像的路径。 For example ImgPath =./TestData/TestWhole\XXXXX.jpg when I print.例如,当我打印时ImgPath =./TestData/TestWhole\XXXXX.jpg XXXXX is 00000 to 82700 in my case在我的情况下,XXXX 是 00000 到 82700
  • ImgPath is the path to images at once. ImgPath 是一次图像的路径。 For example ImgPaths =./TestData/TestWhole\\XXXXX.jpg when I print.例如,当我打印时ImgPaths =./TestData/TestWhole\\XXXXX.jpg But I think it is the entire 00000-XXXXX in one, since ImgPaths is assigned like above.但我认为它是整个 00000-XXXXX 合二为一,因为ImgPaths像上面一样分配。

So, there are 82700 files to handle, and it takes 24 seconds for one file.因此,有 82700 个文件要处理,一个文件需要 24 秒。 Wait for it to be done entirely at once is too long.等待它一次完成太长了。 The parent folder is large so I cannot duplicate the parent folder into 10 copies and execute every single copy.父文件夹很大,因此我无法将父文件夹复制为 10 个副本并执行每个副本。 So dividing that for loop may be the way to go.所以划分那个for循环可能是go的方法。 Seems like it is not a normal for loop but an array for loop.似乎它不是普通for循环,而是一个数组for循环。

So, how to make an array for loop start and stop at a certain element?那么,如何让一个数组for循环在某个元素处开始和停止呢? If I want to start from 11111.jpg and stop at 22222.jpg, how to do it?如果我想从 11111.jpg 开始,到 22222.jpg 停止,怎么办?

This is the source code https://drive.google.com/file/d/1jnE7kSK2eyXEAsTXb_4IctUEbBkZSWJa/view?usp=sharing这是源代码https://drive.google.com/file/d/1jnE7kSK2eyXEAsTXb_4IctUEbBkZSWJa/view?usp=sharing

Thank you.谢谢你。

You can use a conditional statement to skip processing until i reaches the desired starting number, and then break the loop when i exceeds the ending number:您可以使用条件语句跳过处理,直到i达到所需的起始编号,然后在i超过结束编号时break循环:

for i, ImgPath in enumerate(ImgPaths):
    if i > 22222:
        break
    elif i >= 11111:
        # process ImgPath

or use itertools.islice to limit the iterations to the specified range:或使用itertools.islice将迭代限制在指定范围内:

import itertools
for ImgPath in itertools.islice(ImgPaths, 11111, 22222 + 1):
    # process ImgPath

暂无
暂无

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

相关问题 如何从第0个元素开始向后重新排序python列表? - How to reorder a python list backwards starting with the 0th element? Python - 将浮点数转换为 int 以用作数组索引返回数组的第 0 个元素 - Python - Casting a float as int to be used as array index returns 0th element of the array python 用于在第一个元素没有第 0 个元素开始的循环 - python for loops being started at 1st element no 0th element 如何指定第0个参数以外的python参数 - How to specify python arguments except 0th argument python中如何删除未命名的索引行,将第0行替换为索引行进行数据分析 - How to delete Unnamed index row and replace 0th row as index row in python for data analysis 如何在 PySpark dataframe 的第 0 轴上找到 arrays(数组列)的平均值? - How to find the average of arrays (an array column) on 0th axis in a PySpark dataframe? 如何从 tensorflow 中的 2d boolean 掩码沿第 0 轴找到“下一个”值 - How to find "next" value along 0th axis from 2d boolean mask in tensorflow 如何从第 0 个日期开始获得每 3 个日期 - How to get the every 3rd date starting from the 0th one 使用python从数组中查找第4个最大元素 - finding 4th largest element from array using python 想要通过从第 0 个索引开始在其存在旁边插入每个元素直到满足条件来使列表等于目标长度 - Want to make a list equal to a target length by inserting every element next to its presence starting from 0th index until condition is satisfied
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM