Python:索引5324超出了轴0,大小为5040

时间:2016-01-26 11:46:58

标签: python numpy indexing

我有一个Python代码,它从TXT文件读取5040个值来执行模拟,如下所示:

### Starting iteration
h=0.001
for v_capt in v_capt_list:
    print 'Capture velocity =',v_capt, 'm*(s^-2)'
    print 'Fraction of atoms already below the capture velocity =',(F(v_capt)-F(0.0))*100, '%'

    for I in I_list:  
        v_final_3D=[[],[],[],[]]
        result_3D=[[],[],[]]

        for J in J_list:    
            Bfield = ZSField*J

            for det in det_list:
                result=0.0
                for v_min in v_class:
                    s = 0.0
                    v = v_min
                    while s <= 0.524 and v >= 0.0:     # MOT position in the magnetic field files
                        k1=[v,a_function(det,I,v,B(s))]
                        k2=[v+k1[1]*h/2.0,a_function(det,I,v+k1[1]*h/2.0,B(s+k1[0]*h/2.0))]
                        k3=[v+k2[1]*h/2.0,a_function(det,I,v+k2[1]*h/2.0,B(s+k2[0]*h/2.0))]
                        k4=[v+k3[1]*h,a_function(det,I,v+k3[1]*h,B(s+k3[0]*h))]
                        s=s+h/6.0*(k1[0]+2.0*k2[0]+2.0*k3[0]+k4[0])
                        v=v+h/6.0*(k1[1]+2.0*k2[1]+2.0*k3[1]+k4[1])
                    if v <= v_capt and v > 0.0:
                        result=result+F((v_min)+((v_step)/2.0))-F((v_min)-((v_step)/2.0))

                    v_final_3D[0].append(J)
                    v_final_3D[1].append(det)
                    v_final_3D[2].append(v_min)
                    v_final_3D[3].append(v)

                    result_3D[0].append(J)
                    result_3D[1].append(det)
                    result_3D[2].append(result)

然后,代码创建一个名为 B(s)的函数,如下所示(我跳过了第一个和第二个代码之间的部分代码)。

### Reading Magnetic Field Files
path = os.path.dirname(os.path.realpath('__file__'))
# Zeeman slower's file
ZSFile=open(path+'\DyZSField(G)_1A_range354mm_5040points.txt','r')
ZSField = np.array([])
while True:
    line = ZSFile.readline()
    if line == '':
        break
    ZSField=np.append(ZSField,[float(line)])
ZSFile.close()

# Magnetic field as a function of position in meters
def B(s):
    dBds = (Bfield[int(math.ceil(s*1e4))]-    Bfield[int(math.floor(s*1e4))])/1.0e-4 # Gauss*(m^-1)
    return Bfield[int(math.floor(s*1e4))]+dBds*(s-math.floor(s*1e4)*1e-4)

但是,当我运行代码时,Python Shell会显示错误消息IndexError: index 5324 is out of bounds for axis 0 with size 5040(参见下图)。

Error message

我发现了类似的错误,但没有答案可以解决我的问题。你能救我吗?

非常感谢,Marcos Miotti

1 个答案:

答案 0 :(得分:1)

我发现了问题:程序尝试在TXT文件上搜索第5324位的值,但只有5040个数字。因此,我稍微降低了myfunc(&{.field1=23, .field2="myname" }); 值。在上面的第一个代码中,我写了

/api/books?ids[]=1&ids[]=2

然后,我改为

author.get('books')

为了我的代码目的,该解决方案运行良好。的:)