在循环期间,如何使用specifics索引执行例程?

时间:2018-07-23 19:23:29

标签: python arrays loops

所以我有一个任意长度的数组。我想遍历它并获取重要信息,例如索引及其值。例如,我想从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....

任何帮助表示赞赏 谢谢!

1 个答案:

答案 0 :(得分: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])