我创建了一个名为'j'的变量,它具有一些值,我希望我的代码一次选择一个值并执行。 我尝试编写代码,但是没有用。我正在分享我的代码,请查看何时可以对其进行改进。
j = ['0', '1', '3', '4', '6', '7', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67']
for i in j:
labels('i') = mne.read_labels_from_annot('sub-CC721377_T1w', parc='aparc', subjects_dir=subjects_dir)['i']
done
答案 0 :(得分:0)
根据MNE documentation,函数read_labels_from_annot
返回标签列表。
因此,您不必捕获结尾的...)[0]
索引,而应该捕获整个列表:
labels = mne.read_labels_from_annot(...)
这将捕获标签列表,而不是单个标签,从而具有“在0到67的“ [0]”末尾索引”的作用。
您询问是否将所有结果加到一个label_all
变量中。您没有指定(而且我对MNE软件包一无所知),所以不清楚:标签是否会重复?每个输入文件中是否可能出现“ lab123”?如果是这样,label_all
应该存储相同值的多个副本,还是仅存储唯一的标签名称?
我认为您追求的是这样的东西
import mne
def get_labels_for_subject(sub, *, hemi='both', parc='aparc', **kwargs):
"""Get MNE labels for a given subject. **kwargs allows passing named
parameters like subjects_dir, regexp, and others that default to None."""
labels = mne.read_labels_from_annot(sub, hemi=hemi, parc=parc, **kwargs)
return labels
# List of all the subjects
subjects = [
'sub-CC721377_T1w',
'sub-next???',
]
label_all = []
for s in subjects:
label_all.extend(get_labels_for_subject(s, subjects_dir='.'))
print("Got labels:", label_all)