Home > OS >  why list index out of range in the matrix
why list index out of range in the matrix

Time:10-07

IndexError: list assignment index out of range

I can not fix.list assignment index out of range. Can you help me ?

z = [0.5, 0.4]
P = 500  #pisa
T = 400 # Ranking
Tc = [343.01, 549.58]
Pc = [667.03, 706.62]
w = [0.011, 0.099]
kij = [[0, -0.0026], [-0.0026, 0]]

Tr = []
Pr = []
k = []
for i in range(0, 1):
    Tr[i] = T / Tc[i]
    Pr[i] = P / Pc
    k[i] = math.exp(5.37 * (1   w[i]) * (1 - (1 / Tr[i]))) / (Pr[i])
    print(k)

error:

Traceback (most recent call last):
  File ".../tmp.py", line 13, in <module>
    Tr[i] = T / Tc[i]
IndexError: list assignment index out of range

CodePudding user response:

Tr, Pr and k are empty lists. Assigning to an index in them will generate an error.

>>> a = []
>>> a[0] = 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range

Try initializing these lists with length 1.

Tr = [None]
# etc.

CodePudding user response:

Tr is initially empty, so when you try to assign Tr[0] = T/Tc[i] this fails as there is no index 0.

Use append:

Tr.append(T/Tc[i])

Same thing for Pr and k.

NB. for i in range(0, 1) is just i = 0. You can save the loop. Or maybe you meant for i in range(len(Tc)):?

  • Related