我正在使用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
循环获取它。
答案 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