简体   繁体   中英

IndexError:list index out of range in python code

when I'm running this code I have an error and I can't understand whats the problem. this code work like when the (getx )create the array of systems, Cost, weight and expression functions start calculation according to (getx) array. array W is Weight, C is Cost and R is Reliability under the error.

error is:

>IndexError                                Traceback (most recent call last)
<ipython-input-4-0ce5705f90fb> in <module>
     76             if char!= 0 :
     77                 exp = expression(R , possition , char)
---> 78                 wei = Weight(W , possition , char)
     79                 cost = Cost(C , possition , char)
     80                 Total = Total* exp
<ipython-input-4-0ce5705f90fb> in Weight(W, possition, char)
     54 def Weight(W , possition , char):
     55     expW = 0
---> 56     expW = expW + W[possition][int(char)]
     57     return expW
     58 
>IndexError: list index out of range

my code:

import random

def getsys():
    row = ''
    for i in range(0 , 8):
        randintt = str(random.randint(0 , 3))
        row += randintt
    return row


def getx():
    x = []
    for i in range(0,14):
        mysys = getsys()
        x.append(mysys)
    return x 

y = getx()
print (y)


import initialsys
import numpy as np

R = np.array([[0.90 , 0.93,0.91 , 0.95],
               [0.95 , 0.94, 0.93, 0],
               [0.85 , 0.90 , 0.87 , 0.92],
               [0.83 , 0.87 , 0.85 , 0 ],
               [0.94 , 0.93 , 0.95 , 0],
               [0.99 , 0.98 , 0.97 , 0.96],
               [0.91 , 0.92 , 0.94 , 0],
               [0.81 , 0.90 , 0.91 , 0],
               [0.97 , 0.99 , 0.96 , 0.91],
               [0.83 , 0.85 , 0.90 , 0],
               [0.94 , 0.95 , 0.96 , 0],
               [0.79 , 0.82 , 0.85 , 0.90],
               [0.98 , 0.99 , 0.97 , 0],
               [0.85 , 0.92 , 0.95 , 0.99]
              ])
C = np.array([[1, 1, 2, 2],
               [2, 1, 1, 0],
               [2, 3, 1, 4],
               [3, 4, 5, 0],
               [2, 2, 3, 0],
               [3, 3, 2, 2],
               [4, 4, 5, 0],
               [3, 5, 6, 0],
               [2, 3, 4, 3],
               [4, 4, 5, 0],
               [3, 4, 5, 0],
               [2, 3, 4, 5],
               [2, 3, 2, 0],
               [4, 4, 5, 6]
              ])
W = np.array([[3,4,2,5],
               [8,10,9,0],
               [7,5,6,4],
               [5,6,4,0],
               [4,3,5,0],
               [5,4,5,5],
               [7,8,9,0],
               [4,7,6,0],
               [8,9,7,8],
               [6,5,6,0],
               [5,6,6,0],
               [4,5,6,7],
               [5,5,6,0],
               [6,7,6,9]
              ])

def expression(r ,possition , char ):
    exp=1-r[possition , int(char)]
    return exp

def Weight(W , possition , char):
    expW = 0
    expW = expW + W[possition][int(char)]
    return expW

def Cost (C , possition , char):
    expC = 0
    expC =expC + W[possition][int(char)]
    return expC

T = []
W = []
C = []
for z in range(1000):
    x = initialsys.getx()
    possition = 0
    Total = 1.0
    char = ""
    TotalC = 0
    TotalW = 0
    for row in x :
        for char in row :
            if char!= 0 :
                exp = expression(R , possition , char)
                wei = Weight(W , possition , char)
                cost = Cost(C , possition , char)
                Total = Total* exp
                TotalC = TotalC + cost
                TotalW = TotalW + wei
            Total = 1-Total
        end = 1
        end = Total * end
        possition = possition + 1 
        T.append(end)
        W.append(TotalC)
        C.appaend(TotalW)
print(T)

What can I do for this problem??? :(

Quickly reproduced the example given by you and ran it. The problem arises in the very first iteration in this line:

expW = expW + w[possition][int(char)]

where you get an IndexError . The problem is that you initialize W as empty list and pass to the function Weight , where you try to access it's (non existing) content as above.

So I don't know what your code exactly does, but it seems weird that you (quite specificially) initialize W at first and later replace it by an empty list. Maybe there's some error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM