0

So I have an array with arbitrary length. I want to loop over it and getting important information like the index and its value. I would like to, for example, run a subroutine from idx=3..7 (4 idxs) then not execute it for 5 idxs, and then again for idx=13..17 execute and so on..

for example:

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....

any help is appreciated thx!

Nikko
  • 517
  • 3
  • 7
  • 19

1 Answers1

0

Do it with a list of range (or xrange in Python 2) objects.

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])
kindall
  • 178,883
  • 35
  • 278
  • 309
  • thx!. The problem is that the items_to_process are several subelements from array_index, I need to process every n=4 idxs and not to every m=5 idxs. Starting from a n2=arbitrary index, until the end of the array. – Nikko Jul 23 '18 at 19:54