【发布时间】:2015-04-23 21:59:49
【问题描述】:
我在使用叠瓦式循环存储值时遇到了一些问题... 这是一个与我的案例很接近的例子。 为了限制代码的大小,我添加了“a”作为随机值。
对于每个“图像”,我必须计算 dX,如果我能达到我的标准,我会输入一个特定的值 (100),然后我会打破循环进入下一张图像!如果我无法在迭代最大值之前收敛,我会强制使用另一个值 (1),并且我会为下一个图像情况打破循环。
import numpy
res = zeros((len(range(0,5,1)),2)) #array of results
dX = 10. #my important value which allow stop loop for one "image"
n = 0 #number of iteration
itmax = 5. #my iteration max value
#I have to achieve calculations on a great number of cases (image - and i want to store results of each case in "res array")
for image in range(0,5,1):
a = randint(0,9) #for this example, i added a random value to treat the problem
while abs(dX) > 5.:
dX = a - n
if abs(dX) < 5.:
res[image,0] = 100.
res[image,1] = 100.
elif n==itmax:
res[image,0] = 1.
res[image,1] = 1.
break
n = n+1
res
但目前我总是得到零数组,因为它没有发生......
【问题讨论】:
标签: python for-loop numpy while-loop break