简体   繁体   English

for循环在元组中如何工作

[英]How does a for loop work in tuples

I am new to python, having a hard time understanding the following code. 我是python的新手,很难理解以下代码。 It would be great if someone can give an explanation. 如果有人可以给出解释,那就太好了。 I have two tuples. 我有两个元组。 Specifically I cant understand how the for loop works here. 具体来说,我无法理解for循环在这里的工作方式。 And what does weight_cost[index][0] mean. 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

A good practice to get into when you're trying to understand a piece of code is removing the irrelevant parts so you can see just what the code you care about is doing. 当您尝试理解一段代码时,一种好的做法是删除不相关的部分,以便您可以看到自己关心的代码在做什么。 This is often referred to as an MCVE . 这通常称为MCVE

With your code snippet there's several things we can clean up, to make the behavior we're interested in clearer. 使用您的代码段,我们可以清理几件事,以使我们感兴趣的行为更加清晰。

  1. We can remove the loop's contents, and simply print the values 我们可以删除循环的内容,而只需打印值
  2. We can remove the second tuple and the other variables, which we are no longer using 我们可以删除不再使用的第二个元组和其他变量

Leaving us with: 离开我们:

ratios=[(3, 0.75), (2, 0.5333333333333333), (0, 0.5), (1, 0.5)]
for index, ratio in ratios:
  print('index: %s, ratio %s' % (index, ratio))

Now we can drop this into the REPL and experiment: 现在我们可以将其放入REPL并进行实验:

>>> ratios=[(3, 0.75), (2, 0.5333333333333333), (0, 0.5), (1, 0.5)]
>>> for index, ratio in ratios:
...   print('index: %s, ratio %s' % (index, ratio))
... 
index: 3, ratio 0.75
index: 2, ratio 0.5333333333333333
index: 0, ratio 0.5
index: 1, ratio 0.5

You can see clearly now exactly what it's doing - looping over every tuple of the list in order, and extracting the first and second values from the tuple into the index and ratio variables. 您现在可以清楚地看到它的作用-依次遍历列表的每个元组,并将元组的第一个和第二个值提取到indexratio变量中。

Try experimenting with this - what happens if you make one of the tuples of size 1, or 3? 尝试对此进行试验-如果您制作1号或3号元组之一,会发生什么? What if you only specify one variable in the loop, rather than two? 如果仅在循环中指定一个变量而不是两个变量,该怎么办? Can you specify more than two variables? 您可以指定两个以上的变量吗?

The for loop iterates through each tuple in the array, assigned its zero index to index and its one index to ratio. for循环遍历数组中的每个元组,为其分配零索引到index和一个索引到比率。

It then checks the corresponding index in weight_cost, which is a tuple, and checks the zero index of that tuple. 然后,它检查weight_cost中的对应索引(它是一个元组),并检查该元组的零索引。 This is added to weight, and it that is less than or equal to capacity, we move into the if block. 它增加了权重,它小于或等于容量,我们进入if块。

Similarly, index is used to access specific items in other lists, as before. 同样,像以前一样,索引用于访问其他列表中的特定项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM