0

我试图让我的线性规划问题的代码更简单一些。我现在拥有的是:

normal_hours_laptop_01 = LpVariable('Hours for laptop in month 1', 0, 20000)
normal_hours_laptop_02 = LpVariable('Hours for laptop in month 2', 0, 20000)
normal_hours_laptop_03 = LpVariable('Hours for laptop in month 3', 0, 20000)
normal_hours_laptop_04 = LpVariable('Hours for laptop in month 4', 0, 20000)

等等...

我想以如下格式对其进行编码:

d = {}

for x in range(1,13):
    d["production_hours_laptop_{0}".format(x)] = LpVariable("Production hours for laptop in month {}".format(x)]

然后在需要时单独使用这些变量,通过它们的名称和索引号来调用它们。

我可以打印包含所有变量名称的列表,但我不能在计算中使用单个变量。有人知道该怎么做吗?

4

1 回答 1

0
 d = {x: LpVariable("Production hours for laptop in month {}".format(x), 0, 2000) 
      for x in range(1, 13)}

然后d[1]等于LpVariablefor 1。这种技术称为字典理解。

于 2016-10-13T15:39:58.480 回答