我正在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
循环单独获得它。 最佳答案
回应您的评论:现在可以通过单个变量控制:
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