刚开始学习Python,但我无法想出这背后的逻辑,我希望有人能澄清。
# this works without a counter
Numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for Numbers in Numbers:
print(Numbers)
但是
# this doesn't work without a counter
Numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
while Numbers in Numbers <= 5:
print(Numbers)
,“为循环”可以通过每个值进行进步并报告它,但是'while-loop'不能吗?
为什么“循环”需要一个计数器变量才能进步?
我是否正确理解这一点,如果是这样,是否有任何不连续性的实际原因?
编辑:我感谢有关循环如何工作的评论,但这并不是我的问题。考虑到这一点,我想我想出了一种更好的方法来提出问题,再次,如果这一切都毫无根据,我深表歉意,但是我只有3天前才开始学习编码/Python。因此,以前,我是从为什么循环的方向上脱颖而出循环?
#convince me this isn't a for loop, and if it is, why do for loops exist?
Numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
x = 0
while x < len(Numbers):
print(Numbers[x])
x = x + 1
Just starting out with learning Python, but I can't come up with the logic behind this and I was hoping someone could clarify.
# this works without a counter
Numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for Numbers in Numbers:
print(Numbers)
But
# this doesn't work without a counter
Numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
while Numbers in Numbers <= 5:
print(Numbers)
So 'for loop' can progress through each value and report it, but 'while-loop' can't?
Why does 'while loop' require a counter variable to progress when for doesn't?
Am I understanding this correctly, and if so, is there any practical reason for the discontinuity?
EDIT: I appreciate the comments about how the loops work but that's not really my question. After thinking about it, I think I came up with a better way to ask my question, and again, I apologize if this all comes off as hopelessly ignorant, but I only started learning coding/python 3 days ago. So previously I was coming at it from the direction of why while-loops don't have similar functionality to for-loops, so here's the same question from the opposite direction... Why do for-loops exist if they are just a simplified while-loop?
#convince me this isn't a for loop, and if it is, why do for loops exist?
Numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
x = 0
while x < len(Numbers):
print(Numbers[x])
x = x + 1
发布评论
评论(4)
<代码>对于循环基本上是一个一个一个和
,而loop
有点不同,它处理数据直到您的条件为真。在这里,我们将计数器用作条件,因此循环
条件会失败循环可以停止工作。因此,基本上,如果您想一个一个一个数据处理每个数据,则可以使用 ,并且如果要处理数据到某种条件,则可以使用 。前任。如果您有5个元素,并且必须处理所有内容,则可以选择for Loop
,并且如果要多个时间或动态序列处理数据,则可以使用 时使用。
for loop
is basically process each element one by one andwhile loop
is little bit different, it process the data until your condition is true. Here we used counter as condition so whenwhile loop
condition going to fail loop can stop working. So basically if you want to process each data one by one you can go forfor loop
and if you want to process data up to some certain condition you can go forwhile loop
. ex. if you have 5 element and you have to process all then you can go forfor loop
and if you want to process data multiple time or in dynamic sequence you can usewhile loop
.我认为这里的脱节在于,当
for
循环不能时,通常会使用while
循环。这是一个简单的例子:在这种情况下,计数器是没有意义的,因为我们不知道循环会重复多少次。我们也许能够创建一个等效的
for
循环,但它会变得不必要的复杂。I think the disconnect here is that a
while
loop is often used in cases when afor
loop cannot. Here's a simple example:In a case like this, a counter is meaningless because we have no idea how many times the loop will repeat. We might be able to create an equivalent
for
loop, but it would be unnecessarily complicated.这个问题已经得到了很好的回答,因为程序员通常需要三种类型的循环:
for 循环非常适合第一种类型的迭代。您可以在范围或任何其他可迭代对象上进行迭代,例如列表、集合、字典、过滤器等。
while 非常适合第二种类型,因为 while 在循环中测试一次是否终止。与 VBA 不同的是,while 总是在循环的开头完成(而在 VBA 中,您可以“DO”“While”将测试移动到循环的末尾)。您可以使用 while True, if: break 将测试移动到循环中的任何位置,但恕我直言,这在语法上并不优雅。
第三种迭代类型可以使用 for 或 while(或来自 itertools 的 takewhile)来实现。例如,使用范围 = 最大迭代次数的 for 循环,然后在达到结果时“中断”出循环,或者如果迭代未收敛到结果,则使用带有单独计数器的 while 来“超时”。这是我的代码中的一个示例,我想尝试最多 3 次才能在正确的范围内得到错误:
在这段代码中,我利用 := 运算符在 while 循环内分配 eff_r 和 match_loop,所以我总是有即使循环迭代 0 次,退出时的最终 eff_r 值也是如此。循环体只是为下一次迭代设置 Target_p。我希望得到 -0.1 和 0.1 之间的 eff_r,并且会尝试 0 次(如果 switch self.Match 为 False)或最多 3 次。循环的实际输出是aiim_p,它要么不变地通过,要么在每次迭代中进行修改。
最后还有一种较少被提及的迭代方法,这就是递归。这可能非常优雅,但我将其留给读者研究,因为它超出了这个问题的范围。
This has already been answered quite well in that there are normally three types of loops that programmers need:
The for loop is ideal for the first type of iteration. You can iterate on a range or any other iterable e.g. a list, a set, a dict, a filter etc.
while is ideal for the second type as while tests once in the loop whether to terminate or not. Unlike VBA the while is always done at the head of the loop (whereas in VBA you could "DO" "While" to move the test to the end of the loop). You can move the test anywhere in the loop using while True, if: break but IMHO this is syntactically inelegant.
The third iteration type could be implemented with either for or while (or takewhile from itertools). E.g use a for loop with a range = maximum number of iterations then "break" out of the loop when the result is achieved, or use a while with a separate counter to "time out" if the iteration did not converge to the result. Here is an example from my code where I want to try up to 3 times to get an error within the right bounds:
In this code I take advantage of the := operator to assign eff_r and match_loop inside the while loop, so I always have the final eff_r value on exit even if the loop iterates 0 times. The body of the loop just sets up aim_p for the next iteration. I am hoping to get an eff_r within -0.1 and 0.1, and will try either 0 times (if the switch self.Match is False) or up to 3 times. The actual output of the loop is aim_p which either passes through unchanged or gets modified on each iteration.
There is a final, less talked about iteration method and this is recursion. This can be really elegant but I leave it to the reader to research as it is beyond the scope of this question.
a ,而 loop会循环直到条件为false和a for loop将循环循环,直到您通过列表中的所有元素循环循环。一些展示了如何使用它们的方式:
一个打印数字0到9的循环。
number
将在列表中引用列表numbess
a while loop打印数字0 0到5。
i
将引用列表中的位置。另外添加了一个示例,请参见下面的评论:
A while loop will loop until the condition is False and a for loop will loop until you have looped through all elements in the list. Some exemples how you can use them:
A for loop that prints the numbers 0 to 9.
Number
will reference the element in the listNumbers
A while loop that prints the numbers 0 to 5.
i
will reference the position in the list.One more example added, see comment below: