有人可以解释我收到此错误的原因吗?
from netCDF4 import Dataset
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
datapath = r"C:\Users\matth\Downloads\MYD04_L2_v6.0_110E155E_045S010S.A2010_calcv2_dod_flg1.nc"
f = Dataset(datapath)
for i in range(0, 30):
dod = f.variables['dod_modis_flg1'][i]
dod[dod == 0] = np.nan
def nan_if(arr, value):
return np.where(arr == value, np.nan, arr)
mean = np.nanmean([nan_if(dod, -9.99)])
print(mean)
#print(np.nanmax(dod))
#print(np.nanmin([nan_if(dod, -9.99)]))
dod_high = dod[(dod > mean) & (dod != 0)]
anomalies = []
for val in dod_high:
if val > mean:
#print(anomalies)
dod_high_indices1 = np.where((dod > mean) & (dod != 0))
dod_high_indices2 = np.array(np.where((dod > mean) & (dod != 0))).T
anomalies_ind = []
for ind in dod_high_indices2:
anomalies_ind.append(ind)
print(np.asarray(anomalies_ind))
输出:
%run "C:/Users/matth/dod_anomalies.py"
File "C:\Users\matth\dod_anomalies.py", line 26
dod_high_indices1 = np.where((dod > mean) & (dod != 0))
^
IndentationError: expected an indented block
在我看来,我的代码缩进是正确的...出于某种原因,我继续得到这个错误。
答案 0 :(得分:3)
Python期待
后的东西if val > mean:
它忽略了评论的块。如果你有一个像这样的空if语句,只需输入pass,所以python知道它就在那里。
if val>mean:
#print(anomalies)
pass
答案 1 :(得分:1)
if
需要一个正文,而且
for val in dod_high:
if val > mean:
#print(anomalies)
评论不计算在内。您可以创建正文pass
,或者注释if
(或整个循环),但在上下文中,您可能会遇到更严重的问题。即使没有注释,print
也只会打印[]
。