[英]Index out of range while accessing index of the next iteration in loop in python
There are several image files in a directory on which I need to iterate through, where need to access the next iteration value in the current iteration. 我需要在目录中进行迭代的几个图像文件,需要在其中访问当前迭代中的下一个迭代值。
img_files_list = os.listdir(/path/to/directory)
new_list = []
for index, img in enumerate(img_files_list):
if img in new_list:
continue
new_list.extend([img, img_files_list[index + 1], img_files_list[index + 2]])
print(img)
print(img_files_list[index + 1])
print(img_files_list[index + 2])
I need to iterate over all the items of the img_files_list but when reached at the end of the loop need to properly come out of loop without index out of range exception . 我需要遍历img_files_list的所有项,但是当在循环末尾到达时,需要正确地退出循环,而不会出现索引超出范围的异常 。 Anyone, please point out where do I missed the logic.
任何人,请指出我在哪里错过了逻辑。
Seems more like Code Review than Stack Overflow. 看起来更像是代码审查,而不是堆栈溢出。
When you reach the last item in your for image, img in enumerate(img_files_list)
line, there is no item after the last one. 当您到达
for image, img in enumerate(img_files_list)
的最后一项时for image, img in enumerate(img_files_list)
行中的for image, img in enumerate(img_files_list)
最后一项之后没有任何项。 This is what causes an IndexOutOfRangeException
. 这就是导致
IndexOutOfRangeException
原因。
There are a few ways to approach this: 有几种方法可以解决此问题:
As Sruthi V. wrote, include a condition: 正如Sruthi V.所写,包括一个条件:
print(img_files_list[i]) if i + 1 < len(img_files_list): print(img_files_list[i + 1]) if i + 2 < len(img_files_list): print(img_files_list[i + 2])
Include a try... except
: try... except
:
try: print(img_files_list[i]) print(img_files_list[i + 1]) print(img_files_list[i + 2]) except IndexOutOfRangeException: pass
Limit the range of your loop: 限制循环范围:
i_img = list(enumerate(['a', 'b', 'c', 'd', 'e'])) for i, img in i_img [:len(i_img ) - 2]: print(img) print(i_img[i + 1]) print(i_img[i + 2])
Note that you have no reason to use enumerate
— you're just using it to get indices. 请注意,您没有理由使用
enumerate
-只是使用它来获取索引。 You also don't need your if img in new_list
— you're just using it to skip the next two. 您也不需要
if img in new_list
的if img in new_list
您只是使用它来跳过接下来的两个。 You can make this more elegant and solve your problem using range (a modified solution 3). 您可以使它更加优雅,并使用范围(解决方案3)来解决您的问题。
imgs = os.listdir('/path/to/directory')
triplets = []
for i in range(0, len(imgs) - 2, 3):
triplet = [imgs[i], imgs[i + 1], imgs[i + 2]]
triplets.extend(triplet)
print('\n'.join(triplet))
NB I'm not sure what you're even trying to do. 注意:我不确定您要做什么。 Even your
if img in new_list
might be a mistake since it just skips the last 1-2 final items. 即使您
if img in new_list
的if img in new_list
可能是错误的,因为它只跳过了最后1-2个最后一项。 And if that were gone, all you would be doing is making a copy of this list and printing its contents. 如果不存在,您要做的就是复制此列表并打印其内容。 In which case I would suggest:
在这种情况下,我建议:
imgs = os.listdir('/path/to/directory')
print('\n'.join(imgs))
But if you clarify what you're trying to do, I can edit this answer. 但是,如果您弄清楚自己要做什么,我可以编辑此答案。
From my comment to your question: 从我的评论到您的问题:
index + 1
andindex + 2
cause a problem for last 2 items.index + 1
和index + 2
导致最后2个项目出现问题。 For example, if your list's size is 10, then indexes will be 0,2,...,9.例如,如果列表的大小为10,则索引将为0,2,...,9。 When the index is 8 and 9, you are trying to execute
img_files_list[10]
andimg_files_list[11]
.当索引为8和9时,您尝试执行
img_files_list[10]
和img_files_list[11]
。 So the index is out of range.因此索引超出范围。
I assume that you want to access images as 3-item list, then I suggest you a little bit different way. 我假设您想以3项列表的形式访问图像,那么我建议您采取一些不同的方式。 I will use an integer array instead of images for a better understanding.
为了更好的理解,我将使用整数数组而不是图像。
img_files_list = [0,1,2,3,4,5,6,7,8,9]
new_list = []
n = 3
for i in range(0, len(img_files_list), n): # Respectively, i is 0,3,6,9,...
temp = img_files_list[i:i+n]
print(temp)
new_list.extend(temp)
Output: 输出:
[0, 1, 2]
[3, 4, 5]
[6, 7, 8]
[9]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.