python - 键错误:0 Python

标签 python keyerror

嗨,我在 python 方面还是很新,想知道为什么这一行:

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

导致错误: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])

最佳答案

[x, y] 索引与[x][y] 索引绝对不同。前者导致使用元组索引的单一维度,而后者导致参差不齐的二维数组。

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

关于python - 键错误:0 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46979360/

相关文章:

python - Pycharm - 未安装类型提示

python - 在 Replit.com 上使用 youtube_dl 时出现“Dislike_count”错误

python - Pandas 错误 : __setitem__() doesnt recognize dictionary values as a list of column names

python - 我的 Instagram 机器人不喜欢任何帖子并以 KeyError : 0 结束 session

python - 按行过滤数据框

python - 使用 asyncio 有两个无限任务

python - 在 Raspberry Pi 上找不到 ttyUSB0

python - 通过 telethon 查找 session ID 并终止 session

python - apiclient.discovery.build POST 请求正文

python - key 错误:('1', 'occurred at index 0')