KeyError:0 Python

Fin*_*ino 2 python keyerror

嗨,我仍然是python的新手,想知道为什么这行:

    RP[p][t] = demand[p][t] / fillingrate[p]
Run Code Online (Sandbox Code Playgroud)

导致错误:KeyError:0

它遵循代码的相关部分.这只是一个记号错误或解决它的最佳方法是什么?

    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])
Run Code Online (Sandbox Code Playgroud)

Ign*_*ams 5

索引的[x, y]绝对不一样的,通过索引[x][y].前者导致使用元组索引的单个维度,而后者导致粗糙的2-D阵列.

您需要在[x]包含所需值的索引处创建新对象.