我在 Python 中使用 while 循环,如下所示:
t=0
while t < len(ds.time):
if t == 0:
za = ds.isel(time=t)
abc = get_grids_inside_rad(127.2, 15.7)
print(abc)
if t == 1:
za = ds.isel(time=t)
ghi = get_grids_inside_rad(abc.lon,abc.lat)
print(ghi)
else:
break
t += 1
我愿意:
get_grids_inside_rad(127.2, 15.7)
是一个读取 lon 和 lat 的函数。
目前的输出是这样的。但我想要的是最终输出中有四个 lat lon msl。这甚至不会打印第二个 if 语句的输出。
lon lat time msl
17 127.5 15.0 2015-10-16 99847.414062
在 Python 中执行此操作的最佳方法是什么?
len(ds.time)
等于 4。
你只需要在 while 循环范围之外创建变量
abc
,这样当 t==1 并且没有错误出现时它就会有用
t=0
abc = None
while t < len(ds.time):
if t == 0:
za = ds.isel(time=t)
abc = get_grids_inside_rad(127.2, 15.7)
print(abc)
if t == 1:
za = ds.isel(time=t)
if abc is None:
# do something here <- t=0 not executed, execute that first
ghi = get_grids_inside_rad(abc.lon,abc.lat)
print(ghi)
else:
break
t += 1