Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KeyError: 0 Python

Hi I'm still pretty new in python and would like to know why this line:

    RP[p][t] = demand[p][t] / fillingrate[p]

is leading to the error: KeyError: 0

it follows the relevant part of the code. Is it just a mistake of notation or what's the best way to solve it?

productname = ('milk', 'yoghurt', 'buttermilk')
fillingrate = (1.5, 1.0, 2.0)

day = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)

demand = [
(5, 4, 3, 3, 4, 9, 13, 5, 4, 4, 3, 5, 10, 12),
(3, 5, 3, 5, 5, 4,  3, 4, 3, 4, 3, 4,  5,  5),
(3, 3, 5, 4, 4, 5,  4, 3, 4, 3, 4, 5,  4,  5)
]

T = range (len(day))
P = range (len(productname))


for p in P:
   for t in T:
        RP[P,T] = model.addVar (lb = 0, vtype = GRB.CONTINUOUS,
        name = 'Regular Production[' + str(p) + ',' + str(t) + ']')
        print(demand[p][t])
        print(fillingrate[p])
        RP[p][t] = demand[p][t] / fillingrate[p]
        print(RP[p][t])
like image 944
Fino Avatar asked Oct 22 '25 04:10

Fino


2 Answers

Indexing by [x, y] is absolutely not the same as indexing by [x][y]. The former results in a single dimension indexed using a tuple, whereas the latter results in a ragged 2-D array.

You will need to create a new object at the index [x] containing the values you want.

like image 138
Ignacio Vazquez-Abrams Avatar answered Oct 23 '25 20:10

Ignacio Vazquez-Abrams


The expression P is not the same as p. In your code, the first is a range, the second is an integer from that range.

So, the expression RP[P,T] is wrong in two ways: it uses the wrong subscripts, and combines them the wrong way (as another answer points out).

I think you want RP[p][t] instead.

like image 40
Jim DeLaHunt Avatar answered Oct 23 '25 19:10

Jim DeLaHunt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!