我在寫一段代碼來過濾一些數據,並且遇到了一個問題。我目前有:如何在這種情況下表達j + 1?
def accountforfilter(wavelength, flux, filterwavelength, throughput):
filteredwavelength=[]
filteredflux=[]
for i in wavelength:
for j in filterwavelength:
if wavelength==filterwavelength:
j=filterwavelength.index(wavelength)
filteredwavelength.append(wavelength)
filteredflux.append(flux*throughput)
elif filterwavelength<wavelength<filterwavelength(j+1):
filteredwavelength.append(wavelength)
filteredflux.append(flux*f(wavelength))
#f is a function that interpolates using y=mx+c when wavelength and filterwavelength are not the same
elif wavelength<filterwavelength:
i+=1
elif wavelength>filterwavelength:
j+=1
return filteredwavelength, filteredflux
我的問題是與線:
elif filterwavelength<wavelength<filterwavelength(j+1):
這行不行,我得到的錯誤:
TypeError: 'list' object is not callable
如果我改變圓括號到方括號,即
elif filterwavelength<wavelength<filterwavelength[j+1]:
我得到的錯誤:
TypeError: list indices must be integers, not float
我不知道如何表達我的意思用另一種方式。
您必須在您的if語句中首次提及'filterwavelength'。 – xnx 2014-12-07 17:08:41
'for filter in filterwavelength','j = filterwavelength.index(wavelength)' - 你應該使用更多的描述性變量名來避免這些錯誤。 – poke 2014-12-07 17:09:26
不重用該變量。 'j + = 1'沒有意義。另外,請記住,'i + = 1''' i'會在稍後「重置」。做一個調試會話。 – 2014-12-07 17:09:52