简体   繁体   English

请问我该如何解决这个错误(IndexError:list index out of range)?

[英]How could I solve this error please (IndexError: list index out of range)?

I have the following work in Python.我在 Python 中有以下工作。 I'm trying to solve an equation by using Python and showing the results by plotting it.我正在尝试使用 Python 求解方程并通过绘制它来显示结果。 When I run my following below code, I got this error below:当我运行以下代码时,出现以下错误:

The error:错误:

Traceback (most recent call last):
  File "C:\Users\raineen\Desktop\Raneen_Python\Sigma_theta_c.py", line 44, in <module>
    segma.append((((Tt*Ct[count])/(2*t0*lambda1))*((m.log(item*r))**Nt[count]))+(((Tb*Cb)/(2*t0*lambda1))*((m.log(item*r))**Nb))+((Tt*Ct[count])/(t0*lambda1*(Nt[count]+1)))*((m.log(item*r))**(Nt[count]+1)-(m.log(item*R0))**(Nt[count]+1))+ ((Tb*Cb)/(t0*lambda1*(Nb+1)))*((m.log(item*r))**(Nb+1)-(m.log(item*R0))**(Nb+1)))
IndexError: list index out of range

I have three lists and I'm looping through them at the same time, they are:我有三个列表,我同时遍历它们,它们是:

t = [x/100 for x in range(1, 201)] # [0.01,0.02,0.02,..........,2]

Ct=  [126.0, 127.82549463360013, 129.67743712955985, 131.55621066590138, 133.46220397215035, 135.39581140976628, 137.35743305373825, 139.34747477536274, 141.36634832622116, 143.41447142337327]

a=  [39.960039960039964, 19.960079840319363, ..........]

Could I get any help to fix this problem, please?请问我能得到任何帮助来解决这个问题吗?

The Python蟒蛇

import math as m
import matplotlib.pyplot as plt

lambda1 = 1
t = [x/100 for x in range(1, 201)]
t0 = 2
Tt = 1.5
Kt = 126
Kb = 1261
Rt = 5
Nt = [x/10 for x in range(0, 10)]
Nb = 0.36
Tb = 0.5
r = 6.5


R0 = Rt + t0
z = 2/m.sqrt(3)
#Ct = Kt*(z**Nt)
Cb = Kb*(z**Nb)


print('t= ', t)
print('Nt= ', Nt)
Ct = []
for n in Nt:
    Ct.append(Kt*(z**n))

Rm = []
for j in t:
    Rm.append(5+(j/2))

print('Rm= ',Rm)
print('Ct= ',Ct)

a = []
for k,i in zip(Rm,t):
    a.append(t0/(k*i))
    print(k, i)
print('a= ',a)

segma = []
for count, item in enumerate(a):
    segma.append((((Tt*Ct[count])/(2*t0*lambda1))*((m.log(item*r))**Nt[count]))+(((Tb*Cb)/(2*t0*lambda1))*((m.log(item*r))**Nb))+((Tt*Ct[count])/(t0*lambda1*(Nt[count]+1)))*((m.log(item*r))**(Nt[count]+1)-(m.log(item*R0))**(Nt[count]+1))+ ((Tb*Cb)/(t0*lambda1*(Nb+1)))*((m.log(item*r))**(Nb+1)-(m.log(item*R0))**(Nb+1)))

print('Sigma_theta_c = ', segma)

##for i in segma:
##    print(i)

plt.plot(t,segma)

plt.xlabel('t')
plt.ylabel('Sigma_theta_c')


plt.show()

This is below the equation:这是下面的等式:

在此处输入图片说明

我检查你的代码和问题是长度CtNt阵列是10 ,而名单的长度a是200。在这种情况下你列举的清单a使用count来访问列表元素CtNt这导致IndexError

General way to solve similar problems like this - check out length of the lists you are accessing, creating.解决类似问题的一般方法 - 检查您正在访问、创建的列表的长度。 Could you share full equations and your domain model?你能分享完整的方程和你的领域模型吗? What exactly are you solving?你到底要解决什么问题? What's the desired input and output?所需的输入和输出是什么?

len(t) # 200长度(吨)#200

len(segma) # 10 len(segma) # 10

plt.plot(segma) works and actually creates the plot plt.plot(segma) 工作并实际创建绘图

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决错误“IndexError:列表索引超出范围” - How do I solve the error, "IndexError: list index out of range" IndexError:列表索引超出范围,请问我该如何解决? - IndexError: list index out of range please how can i fix it? 如何解决IndexError:列表索引超出范围? - How to solve IndexError: list index out of range? 如何解决这个 IndexError: list index out of range - How to solve this IndexError: list index out of range 如何解决此错误:“builtins.IndexError: list index out of range”? - How can I solve this error: “builtins.IndexError: list index out of range”? 如何解决此错误:IndexError:列表索引超出范围 - How can I fix this error: IndexError: list index out of range 我收到错误 [IndexError: list index out of range] 但请帮我解决这个问题 - I am getting error [ IndexError: list index out of range ] but please help me to resolve this 如何解决 IndexError: list index out of range in a list of dict? - How can I solve IndexError: list index out of range in a list of dict? 如何解决python中的以下错误:“ IndexError:字符串索引超出范围” - How to solve the following error in python: “IndexError: string index out of range” 读取 CSV 文件时如何解决“IndexError: list index out of range”错误 - How to solve "IndexError: list index out of range" error when reading an CSV file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM