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.

3
  • you mean varlist[2:3]? Commented Apr 25, 2017 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. Commented Apr 25, 2017 at 10:08
  • @jyotish: Thanks. I could combine the for and if using the generator expressions and make it work. I feel this is still a way around. I hope that someone would provide a direct answer for the question. Commented Apr 25, 2017 at 11:08

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.