我正在 Python 中读取一个 Matlab mat
文件,其中包含三个数组:tom、dick 和 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
现在我必须调试,并且不想访问 for
循环的所有三个 varlist
。如何只为 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
关于python - 访问Python列表中的特定变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43607480/