本文介绍了Python中用于循环的多个循环控制变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我碰到一个情况,我需要实现一个for循环与多个循环控制变量。基本上这就是我想要做的事情:



  for(int i = 0,j = n; i  = 0; i ++,j--)
做我的东西

我如何在Python中执行此操作?



pre $ b $ b做我的东西

但是这不起作用。这里的正确语法是什么?另外,还有一个更优雅的(pythonic)构造的用例吗?

解决方案

对于您的具体示例,看起来你真的需要在循环定义本身中单独的变量。你可以迭代i值并且通过构造j值来构造j值:

 对于范围(0,n)中的i, :
j = ni
#做东西

一般来说,在语句中为语句指定多个值,如果它们相互依赖的话。相反,使用for来遍历一些基值或其他值可以派生的值。

You 可以指定多个值在 for 循环中,但是它们必须从一个单独的迭代中获取:

  for i,j in zip(range(0,n),range(n,0,-1)):
#do stuff

这需要我从第一个范围(0到n-1)和j从第二个(n到1)。 zip 创建一个新的迭代器,通过对你赋予它的迭代器的元素进行分组配对。

python for 循环不像Java / C中的循环,它有一个初始化/检查结束条件/更新结构,反复修改某个持久化循环索引变量。对于循环,Python 遍历由源迭代器提供的值的流,每次从源获取一个值。 (这在其他一些语言中类似于 foreach 类型的构造)。无论你想要迭代什么,你需要在开始循环之前把它放到一个迭代器中。换句话说,每个Python for循环都可以被认为是类似于类似于:

pre $ for(i = iterable.getNextValue (); iterable.isNotEmpty(); i = iterable.getNextValue())

循环初始化与循环更新有所不同,除了获取下一个值之外,不能有任何其他操作,并且不能将结束条件设置为是可迭代耗尽以外的任何操作。如果你想做这样的事情,你必须在循环中自己做(例如,通过分配一个次要的循环变量,如在我的例子中,或通过检查自定义循环退出条件和突破),或将其构建到您正在迭代的源代码中。

I came across a situation where I need to implement a for loop with more than one loop control variable. Basically this is what I am trying to do

Java:

for (int i=0,j=n; i<n,j>=0; i++, j--)     
    do my stuff

How do I do this in Python?

for i in range(0,n), j in range(n-1,-1,-1): 
    do my stuff

But this doesn't work. What would be the right syntax here? Also, is there a more elegant(pythonic) construct for the use-case?

解决方案

For your specific example, it doesn't look like you really need separate variables in the loop definition itself. You can just iterate over the i values and construct the j values by doing n-i:

for i in range(0, n):
     j = n-i
     # do stuff

In general, you can't specify multiple values in the for statement if they depend on each other. Instead, use the for to iterate over some "base" value or values from which the others can be derived.

You can specify multiple values in the for loop, but they must be drawn from a single iterable:

for i, j in zip(range(0, n), range(n, 0, -1)):
    # do stuff

This takes i from the first range (0 to n-1) and j from the second (n to 1). zip creates a new iterable by componentwise pairing the elements of the iterables you give it.

The thing to remember is that Python for loops are not like loops in Java/C, which have an initialize/check end condition/update structure that repeatedly modifies some persistent loop index variable. Python for loops iterate over a "stream" of values provided by a "source" iterable, grabbing one value at a time from the source. (This is similar to foreach-type constructs in some other languages.) Whatever you want to iterate over, you need to get it into an iterable before you begin the loop. In other words, every Python for loop can be thought of as roughly analogous to something like:

 for (i=iterable.getNextValue(); iterable.isNotEmpty(); i=iterable.getNextValue())

You can't have the loop initialization be different from the loop update, and you can't have those be any operation other than "get the next value", and you can't have the end condition be anything other than "is the iterable exhausted". If you want to do anything like that, you have to either do it yourself inside the loop (e.g., by assigning a secondary "loop variable" as in my example, or by checking for a custom loop exit condition and breaking out), or build it into the "source" that you're iterating over.

这篇关于Python中用于循环的多个循环控制变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 16:02