SKPS
SKPS

Reputation: 5847

Accessing specific variable in Python list

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.

Upvotes: 1

Views: 215

Answers (1)

Fabian N.
Fabian N.

Reputation: 3856

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

Upvotes: 1

Related Questions