【发布时间】:2017-05-10 02:19:09
【问题描述】:
我是 python 新手,很难理解以下代码。如果有人可以给出解释,那就太好了。我有两个元组。具体来说,我无法理解 for 循环在这里是如何工作的。还有 weight_cost[index][0] 是什么意思。
ratios=[(3, 0.75), (2, 0.5333333333333333), (0, 0.5), (1, 0.5)]
weight cost=[(8, 4), (10, 5), (15, 8), (4, 3)]
best_combination = [0] * number
best_cost = 0
weight = 0
for index, ratio in ratios:
if weight_cost[index][0] + weight <= capacity:
weight += weight_cost[index][0]
best_cost += weight_cost[index][1]
best_combination[index] = 1
【问题讨论】:
-
阅读python教程或python说明书是最好的方法。
标签: python loops for-loop tuples