1

I am reading in Python a Matlab mat file, which contains three arrays: tom, dick and harry. In Python, I use a for loop which does operations on this array list. Following is the demo-code:

import scipy.io as sio
mat_contents = sio.loadmat('names.mat') # with arrays tom, dick and harry
varlist = ['tom', 'dick', 'harry']
for w in varlist:
    cl = mat_contents[w]
    # some more operations in the loop

Now that I have to debug and do not want to access all the three varlist for the for loop. How to run the for loop only for harry? I know varlist[2] gets me harry, but I could not succeed getting it alone for the for loop.

SKPS
  • 5,433
  • 5
  • 29
  • 63
  • you mean `varlist[2:3]`? – ewcz Apr 25 '17 at 10:08
  • 3
    put `if w == "harry": do operations...` inside for loop. Please specify the input and desired output if it doesn't help, it will help to understand the question better. – JkShaw Apr 25 '17 at 10:08
  • @jyotish: Thanks. I could combine the `for` and `if` using the [generator expressions](http://stackoverflow.com/a/6981771/1977614) and make it work. I feel this is still a way around. I hope that someone would provide a direct answer for the question. – SKPS Apr 25 '17 at 11:08

1 Answers1

1

In response to your comment: now controllable with a single variable:

import scipy.io as sio
mat_contents = sio.loadmat('names.mat') # with arrays tom, dick and harry
varlist = ['tom', 'dick', 'harry']

# set it to -1 to disable it and use all arrays
debug_index = -1

# or set it to an index to only use that array
debug_index = 1

for w in [varlist[debug_index]] if debug_index + 1 else varlist:
    cl = mat_contents[w]
    # some more operations in the loop
Fabian N.
  • 3,807
  • 2
  • 23
  • 46
  • This serves the purpose. But it is not what I am looking for. Lets say I would like to debug for `tom`. Can it be made as simple as changing the index and not typing the names for each? – SKPS Apr 25 '17 at 13:26