-1
我正在尝试创建一个函数,用于在给定的CT扫描目录中提取一批患者。在图像分割过程中,某些患者的扫描失败,因此我正在循环查看列表,直到找到成功分割患者的“批号”。但是,当我运行它时,下面的代码无限循环。我不明白为什么'休息'不会终止循环。while循环中尝试/异常命令(Python)
任何想法,将不胜感激!:)
INPUT_FOLDER = "D:\CT\stage1\stage1"
patients = os.listdir(INPUT_FOLDER)
patients.sort()
#start, n_batch are given as parameters.
data = start #initialize index for list 'validation_patients'
valid_patient_list = [] #create an empty list for patient data with successful segmentation
while True: #iterate over variable 'data' until the list of valid patients is completed
try:
x = patients[data]
load_scan(INPUT_FOLDER + '\\' + x)
valid_patient_list.append(data)
if len(valid_patient_list) == n_batch: #escape while loop when the list length is equal to designated batch size
break
else: data += 1 #if the length of list is smaller than the desired batch number, go for the next patient
except IndexError:
data += 1 # go for the next data: do not add this one to the list
continue
#some more code below that deals with the valid_patient_list, but the loop runs infinitely..
我强烈建议你让你的错误处理更加具体,并添加一些反馈 - 目前,你(或其他任何人)不可能知道究竟出了什么问题。 – jonrsharpe
@jonrsharpe感谢您的评论。我添加了一条评论,使我的目的更加明确。我在这里要做的是从数百个病人数据清单中挑选一批病人。我可以按顺序迭代批次,但这不起作用,因为有些患者的数据不起作用。我正在使用try/exception命令来处理这个问题。对不起,我对python比较陌生,所以我不确定这是否回答你的问题。 –
我没有问一个问题。我建议你做更多的调试 - 参见例如https://ericlippert.com/2014/03/05/how-to-debug-small-programs/。 – jonrsharpe