单个for循环中的多个计数器:Python [英] Multiple counters in a single for loop : Python
问题描述
我想要的东西像 - for i,j in x,range(0,len(x)):我知道Python解释这个不同的原因,但是我怎样才能在一个for循环中同时运行两个循环计数器? / p>
您一般需要 zip
,其中包含两个迭代器, S.Mark说。但在这种情况下, enumerate
完全符合你的需要,这意味着你不必直接使用 range
/ p>
对于j,我在枚举(x)中:
请注意,这首先给出了 x
的索引,所以我将 j,i
。
Is it possible in Python to run multiple counters in a single for loop as in C/C++?
I would want something like -- for i,j in x,range(0,len(x)):
I know Python interprets this differently and why, but how would I run two loop counters concurrently in a single for loop?
You want zip
in general, which combines two iterators, as @S.Mark says. But in this case enumerate
does exactly what you need, which means you don't have to use range
directly:
for j, i in enumerate(x):
Note that this gives the index of x
first, so I've reversed j, i
.
这篇关于单个for循环中的多个计数器:Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!