Python 从循环内的数组中删除对象
一般来说,我有以下(当然是简化的)程序结构:Python 从循环内的数组中删除对象,python,arrays,iteration,Python,Arrays,Iteration,一般来说,我有以下(当然是简化的)程序结构: for i in range(len(EdgePixels)): for j in range(len(EdgePixels)): for k in range(len(EdgePixels)): # Now in here I want to delete some Entries from the Array... # e.g. I want to remove EdgePi
for i in range(len(EdgePixels)):
for j in range(len(EdgePixels)):
for k in range(len(EdgePixels)):
# Now in here I want to delete some Entries from the Array...
# e.g. I want to remove EdgePixels[5], so:
del EdgePixels[5]
如果我尝试运行这个(不完全是这4行,但问题是在这4行中),我当然会得到错误
“列表索引超出范围”
问题是,我也在外部2中为
-循环使用数组
我的目标是,我可以从内部for
循环中的数组中删除一些“不必要的”项,而外部2个for循环可以使用“新数组”和删除的项继续运行
有没有办法解决这个问题?简单的办法是忽略索引,而不是删除这些元素:
ignore_indices = set()
for i, item1 in enumerate(EdgePixels):
if i in ignore_indices:
continue
for j, item2 in enumerate(EdgePixels):
if j in ignore_indices:
continue
for k, item3 in enumerate(EdgePixels):
ignore_indices.add(5)
好的,谢谢!我会尝试并报告我的结果好吧,我想,我的程序的这一部分现在可以工作了!谢谢现在我必须完成我计划的另一部分;不客气。请不要忘记投票/接受答案:)哦,对不起!谢谢你的提醒,现在我做到了:)