[英]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 = './TestData/TestWhole'
.在我的情况下TestImgPath = './TestData/TestWhole'
。 TestData
and TestWhole
are folders. TestData
和TestWhole
是文件夹。ImgPath =./TestData/TestWhole\XXXXX.jpg
when I print.例如,当我打印时ImgPath =./TestData/TestWhole\XXXXX.jpg
。 XXXXX is 00000 to 82700 in my case在我的情况下,XXXX 是 00000 到 82700ImgPaths =./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.