Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.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 - Fatal编程技术网

Python 错误:索引超出范围

Python 错误:索引超出范围,python,Python,正如可以看到的,我有1000个θ,sigma的每个部分。但是当我对θ从1迭代到1000时,我得到了一个错误 import time start_time = time.clock() import numpy as np theta=np.linspace(3,100,1000) sigma=np.linspace(0,10,1000) Re=5 import os completeName = os.path.abspath("New Volume (F:)/New Innings 3/S

正如可以看到的,我有1000个θ,sigma的每个部分。但是当我对θ从1迭代到1000时,我得到了一个错误

import time
start_time = time.clock()

import numpy as np
theta=np.linspace(3,100,1000)
sigma=np.linspace(0,10,1000)
Re=5

import os
completeName = os.path.abspath("New Volume (F:)/New Innings 3/Sigma at Re=5 @100 .txt")
file = open("Sigma at Re=5 @100.txt", "w")

for i in np.arange(0,K,1):         //K=1000
    mu=np.sqrt(Re*sigma)
    A=(mu-1)*np.exp(mu)+(mu+1)*np.exp(-mu)
    B=2*mu*(theta[i])
    C=(A/B)

    D1=np.exp(mu)/(2*(mu+sigma))
    D2=np.exp(-mu)/(2*(mu-sigma))
    D3=mu**2
    D4=np.exp(-sigma)
    D5=sigma
    D6=(mu**2)-(sigma**2)
    D7=D3*D4
    D8=D5*D6
    H=D7/D8
    D9=(1/sigma)
    D=D1-D2+H-D9
    K1=C-D
    K2=np.delete(K1,0)
    K3=np.nonzero(K2>0)
    K4=sigma[K3]
    K5=K4[0]
    K55=np.array(K5)

    file.write("%g\n" % K55)

file.close()

print time.clock() - start_time, "seconds"
输出如下:

 33     K3=np.nonzero(K2>0)
 34     K4=sigma[K3]
 35     K5=K4[0]
 36     K55=np.array(K5)

IndexError: index out of bounds corresponding to line 35.

请提供帮助。

因为循环中的
K4
列表变为空,您可以使用IDE调试器进行调试,或者只编写print语句

import time
start_time = time.clock()

import numpy as np
theta=np.linspace(3,100,1000)
sigma=np.linspace(0,10,1000)
Re=5

import os
completeName = os.path.abspath("New Volume (F:)/New Innings 3/Sigma at Re=5 @100 .txt")
file = open("Sigma at Re=5 @100.txt", "w")

for i in np.arange(0,1000,1):         # K=1000
    mu=np.sqrt(Re*sigma)
    A=(mu-1)*np.exp(mu)+(mu+1)*np.exp(-mu)
    B=2*mu*(theta[i])
    C=(A/B)

    D1=np.exp(mu)/(2*(mu+sigma))
    D2=np.exp(-mu)/(2*(mu-sigma))
    D3=mu**2
    D4=np.exp(-sigma)
    D5=sigma
    D6=(mu**2)-(sigma**2)
    D7=D3*D4
    D8=D5*D6
    H=D7/D8
    D9=(1/sigma)
    D=D1-D2+H-D9
    K1=C-D
    K2=np.delete(K1,0)
    K3=np.nonzero(K2>0)
    if len(K4) == 0:
        print "i:",i
        print "K4:",K4
        break 
    K4=sigma[K3]
    K5=K4[0]
    K55=np.array(K5)

    #file.write("%g\n" % K55)

file.close()

#print time.clock() - start_time, "seconds"

它是关于
K3=np.nonzero(K2>0)
语句的,当
i==121
时,它变为0,再次检查您的算法。

尝试找到发生错误的确切行(使用打印语句),以及$i$给出错误的行。