Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 列表索引超出范围_Python_Mysql_List_Numpy_Dictionary - Fatal编程技术网

Python 列表索引超出范围

Python 列表索引超出范围,python,mysql,list,numpy,dictionary,Python,Mysql,List,Numpy,Dictionary,因此,我通过SQL查询调用一些数据,在尝试循环、规范化和绘制数据时,遇到了列表索引范围的错误 以下是我的SQLs: s1 = db.dict.execute("SELECT sp.wavelength, sp.flux, spt.spectral_type, s.designation, s.shortname, s.names FROM spectra AS sp JOIN sources AS s ON sp.source_id=s.id JOIN spectral_types as spt

因此,我通过SQL查询调用一些数据,在尝试循环、规范化和绘制数据时,遇到了列表索引范围的错误

以下是我的SQLs:

s1 = db.dict.execute("SELECT sp.wavelength, sp.flux, spt.spectral_type, s.designation, s.shortname, s.names FROM spectra AS sp JOIN sources AS s ON sp.source_id=s.id JOIN spectral_types as spt ON spt.source_id=s.id WHERE sp.instrument_id=6 AND sp.mode_id=2 AND 10<spt.spectral_type<19").fetchall()
s2 = db.dict.execute("SELECT sp.wavelength, sp.flux, spt.spectral_type, s.designation, s.shortname, s.names FROM spectra AS sp JOIN sources AS s ON sp.source_id=s.id JOIN spectral_types as spt ON spt.source_id=s.id WHERE sp.instrument_id=9 AND sp.wavelength_order='n3' AND 10<spt.spectral_type<19").fetchall()
s3 = db.dict.execute("SELECT sp.wavelength, sp.flux, spt.spectral_type, s.designation, s.shortname, s.names FROM spectra AS sp JOIN sources AS s ON sp.source_id=s.id JOIN spectral_types as spt ON spt.source_id=s.id WHERE sp.instrument_id=16 AND 10<spt.spectral_type<19").fetchall()
最后,我想循环使用它们来规范化、修剪和打印我调用的字典中的所有条目

for n,i in enumerate(S):
    W,F = S[n]['wavelength'], S[n]['flux'] # here I define wavelength and flux from SQL query "S"
    band = [1.15,1.325] #The range at which I want to normalize, see next line
    S1 = at.norm_spec([W,F], band) # here I normalize W and F and define S1 as the normalized W,F
    W3 = S1[n][0] #here I define a new W and F but from the normalized S1 spectrum file
    F3 = S1[n][1] 
    W2,F2 =[p[np.where(np.logical_and(W3>1.15, W3<1.325))] for p in [W3,F3]] #here I trim the noisy ends of data and narrow to a small range
    z = np.polyfit(W2, F2, 3) #from here on it's just fitting polynomials and plotting
    f = np.poly1d(z)
    yvalue = 6*(max(F2)/9)
    xvalue = 6*(max(W2)/9)
    W_new = np.linspace(W2[0], W2[-1], 5000)
    F_new = f(W_new)
    plt.ylabel('Flux F$_{\lambda}$')
    plt.xlabel('Wavelength ($\mu$m)')
    name= S[n]['designation']
    name2= S[n]['shortname']
    name3= S[n]['names']
    plt.annotate('{0} \n Spectral type: {1}'.format(S[n][('designation' or 'shortname' or 'names')], S[n]['spectral_type']), xy=(xvalue, yvalue), xytext=(xvalue, yvalue), color='black')
    #plt.figure()
    plt.plot(W2,F2, 'k-', W_new, F_new, 'g-')
对于枚举中的n,i:
W、 F=S[n]['wavelength',S[n]['flux']#这里我从SQL查询“S”定义波长和流量
band=[1.15,1.325]#我想要标准化的范围,见下一行
S1=at.norm_spec([W,F],band)#这里我规范化了W和F,并将S1定义为规范化的W,F
W3=S1[n][0]#这里我定义了一个新的W和F,但来自规范化的S1光谱文件
F3=S1[n][1]

W2,F2=[p[np.where(np.logical_)和(W3>1.15,W3如果我很好地理解了你的代码,首先你迭代了S的'n'个对象,但是S1是一个全新的对象,它的数量不是“n”,而是其他一些(这个向量的长度,或者别的什么)。也许只是
W3=S1[0]
是正确的吗?不过,我不知道.norm\u spec
中的
是什么,返回的对象是什么类型,所以我只是猜测


建议:在你的代码中写(更多)有意义的变量。读这样的东西真的很难,而且写这样的代码很容易出错。这不是pythonic。

嗨,谢谢你的回答。解决方案实际上是写
W3=S1[0][0]和
F3=S1[0][1] “是的,但是你的解释确实帮助我理解了我错在哪里。
for n,i in enumerate(S):
    W,F = S[n]['wavelength'], S[n]['flux'] # here I define wavelength and flux from SQL query "S"
    band = [1.15,1.325] #The range at which I want to normalize, see next line
    S1 = at.norm_spec([W,F], band) # here I normalize W and F and define S1 as the normalized W,F
    W3 = S1[n][0] #here I define a new W and F but from the normalized S1 spectrum file
    F3 = S1[n][1] 
    W2,F2 =[p[np.where(np.logical_and(W3>1.15, W3<1.325))] for p in [W3,F3]] #here I trim the noisy ends of data and narrow to a small range
    z = np.polyfit(W2, F2, 3) #from here on it's just fitting polynomials and plotting
    f = np.poly1d(z)
    yvalue = 6*(max(F2)/9)
    xvalue = 6*(max(W2)/9)
    W_new = np.linspace(W2[0], W2[-1], 5000)
    F_new = f(W_new)
    plt.ylabel('Flux F$_{\lambda}$')
    plt.xlabel('Wavelength ($\mu$m)')
    name= S[n]['designation']
    name2= S[n]['shortname']
    name3= S[n]['names']
    plt.annotate('{0} \n Spectral type: {1}'.format(S[n][('designation' or 'shortname' or 'names')], S[n]['spectral_type']), xy=(xvalue, yvalue), xytext=(xvalue, yvalue), color='black')
    #plt.figure()
    plt.plot(W2,F2, 'k-', W_new, F_new, 'g-')
--->63         W3 = S1[n][0] #here I define a new W and F but from the normalized S1 spectrum file
64         F3 = S1[n][1]
65         W2,F2 =[p[np.where(np.logical_and(W3>1.15, W3<1.325))] for p in [W3,F3]] #here I trim the noisy ends and narrow to potassium lines

IndexError: list index out of range