python - 访问Python列表中的特定变量

标签 python python-3.x

我正在 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/

相关文章:

python - 如何制作自定义 python 站点包?

python - Keras - 摘要直方图 LSTM 中的 Nan

python - Plon:与非文件夹对象相关的python脚本index_html

python - DictReader 上的 Filter()

python - 使用 Python 的 Beautiful Soup 提取图像

python - pytest-bdd : Importing common steps

python - 在 Python 中创建 Zigzag 数组的函数

python - 如何在 Matplotlib 上绘制两个数据点之间的水平线?

python - 为什么设置窗口图标时没有定义 .ico 文件?

python-3.x - python给视频opencv添加音频