[英]While loop in for loop - For loop does not go through all items
我有一个带有 for 循环的 while 循环。 for 循环遍历所有vel
值,while 循环计算每个vel
值的等效b
值。 它通过增加 b 的值来达到此目的,直到它在不同的代码集中满足特定要求( len([*filter(lambda x: x < 1, radii)]) > 0
)。 当我运行代码时,它只确定了vel
列表中第一项的b
值。 我怎样才能解决这个问题?
def wtf():
vel = np.arange(0, 1, 0.01)
b = 0.1
bvalues = [b]
for i in vel:
print("for velocity = " +str(i))
radii = calcCoords(1, b, i)[1]
while len([*filter(lambda x: x < 1, radii)]) > 0:
b += 0.01
radii = calcCoords(1, b, i)[1]
print (b)
return(b)
bvalues.append(b)
print(bvalues, vel)
return (bvalues, vel)
您在 for 循环的主体中有一个无条件的返回。
def wtf():
vel = np.arange(0, 1, 0.01)
b = 0.1
bvalues = [b]
for i in vel:
print("for velocity = " +str(i))
radii = calcCoords(1, b, i)[1]
while len([*filter(lambda x: x < 1, radii)]) > 0:
b += 0.01
radii = calcCoords(1, b, i)[1]
print (b)
return(b) # <- This is why the for loop runs only once
bvalues.append(b)
print(bvalues, vel)
return (bvalues, vel)
一旦 while 循环运行一次,它就会return
并且for
迭代的 rest 不会运行 - 您的 function 会返回。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.