[英]How to exit a while loop nested in a for loop
我想這應該很簡單。 代碼的第一部分是初始化變量。 我的問題是while循環。 如果條件j == len(time)
達到,我需要退出 WHILE 循環和 FOR 循環。 但問題是,一旦條件為真,while 循環就會再次迭代; 因此,時間超出了索引。
import numpy as np
# Variables to initialize the problem
timeframe = 25
dt=0.025
dat=[(0, 5)]
dat.append((timeframe,dat[len(dat)-1][-1]))
time = np.arange(dat[0][0],dat[-1][0]+dt,dt)
dat_discretized = np.zeros(len(time))
#While inside a for loop
j=0
for i in range(len(dat)):
while dat[i][0] <= time[j] <= dat[i+1][0]:
dat_discretized[j] = dat[i][1]
j += 1
print(j)
if j == len(time):
break
如何在不重新檢查 while 條件的情況下存在 while 循環?
for i in range(len(dat)):
breakFor=false
while dat[i][0] <= time[j] <= dat[i+1][0]:
dat_discretized[j] = dat[i][1]
j += 1
print(j)
if j == len(time):
breakFor=true
break
if breakFor:
break
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.