提前感谢您的帮助。我的if else陈述有问题。下面是我的代码。基本上,如果else:
输入,则意味着没有数据,并且我的其余代码不应运行。我需要做的是,当else:
输入时,我需要它返回新的randx和randy值,然后将其重新输入到中if (dem_arr[randx, randy] > -100):
。我尝试使用时没有成功。
neighbors = [(-1,-1), (-1,0), (-1,1), (0,1), (1,1), (1,0), (1,-1), (0,-1)]
mask = np.zeros_like(dem_arr, dtype = bool)
stack = [(randx, randy)] # push start coordinate on stack
counterStack = [(randx, randy)]
if (dem_arr[randx, randy] > -100):
count = 0
while count <= 121:
x, y = stack.pop()
mask[x, y] = True
for dx, dy in neighbors:
nx, ny = x + dx, y + dy
if (0 <= nx < dem_arr.shape[0] and 0 <= ny < dem_arr.shape[1] and dem_arr[x, y] > -100 and dem_arr[nx, ny] > -100 and not mask[nx, ny] and abs(dem_arr[nx, ny] - dem_arr[x, y]) <= 5): #set elevation differnce
stack.append((nx, ny)) #if point is selected (true) array position gets added to stack and process runs over again
if ((nx, ny) not in counterStack):
counterStack.append((nx, ny))
dem_copy[(nx, ny)] = 8888
dem_copy[randx, randy] = 8888
count += 1
else: #if enters else then need new randx and new randy points need to be returned and re-enter the above if(dem_arr...)
print 'Point chosen has no data'
randx = random.randint(0, row-1)
randy = random.randint(0, col-1)
谢谢您的帮助。
-R
像这样修改您的代码。
前:
if (dem_arr[randx, randy] > -100):
...
else:
...
后:
while(True):
if (dem_arr[randx, randy] > -100):
...
break#break from this while-loop.
else:
...
continue#go back and continue this while-loop.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句