[英]How do I use 2 variables in a Python for loop in the recommended Pythonic way?
I have the following for loop
in C
that I want to mimic in Python: 我在
C
中有以下要在Python中模仿的for loop
:
for (index = 0; index < n; ++index)
{
for (i = 0, j = index; j < n; ++i, ++j)
{
'do something'
}
}
Is there a more elegant/Pythonic way to do this, or do I have to declare a variable outside of the loop like so: 有没有更优雅的/ Pythonic的方式来做到这一点,或者我必须像这样在循环外声明一个变量:
for index in range(m):
i = 0
for j in range(index, m):
'do something'
i += 1
It's hard to say without knowing more about what is going in the second loop, but as it's written I would say: 很难不知道第二个循环中发生的事情就很难说,但是正如我写的那样,我会说:
for index in range(n):
for i,j in enumerate(range(index,n)):
'do something'
would be the way you'd do that. 将是您这样做的方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.