首页 文章

如何在循环中使用特定索引执行例程?

提问于
浏览
0

所以我有一个任意长度的数组 . 我想循环它并获取索引及其值等重要信息 . 我想,例如,运行idx = 3..7(4 idxs)的子程序,然后不执行5个idxs,然后再执行idx = 13..17执行等等 .

例如:

array_index=[0,1,2,3,4,5...n]
if idx==3..7:
     z=subroutine(x,y)
if idx=8..12:
     nothing
if idx=13..17:
      z=subroutine(x,y)
and so on....

感谢任何帮助thx!

1 回答

  • 0

    使用 range (或Python 2中的 xrange )对象列表来执行此操作 .

    array_index = list(range(100))
    
    # note that since `range()` excludes its end value, it's 3-7 and 13-17 here
    items_to_process[range(3, 8), range(13, 18)]
    
    for r in items_to_process:
       for i in r:
           # pass index and value to subroutine (I assume that's what x and y are)
           subroutine(i, array_index[i])
    

相关问题