访问Python列表中的特定变量

时间:2017-04-25 09:59:26

标签: python python-3.x

我正在使用Python阅读Matlab mat文件,该文件包含三个数组:tom, dick and harry。在Python中,我使用for循环来对此数组列表执行操作。以下是演示代码:

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

现在我必须调试并且不想访问varlist循环的所有三个for。如何仅为harry运行for循环?我知道varlist[2]让我harry,但我无法单独为for循环获取它。

1 个答案:

答案 0 :(得分:1)

回应你的评论:现在可用一个变量控制:

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