繁体   English   中英

如何以推荐的Python方式在Python for循环中使用2个变量?

[英]How do I use 2 variables in a Python for loop in the recommended Pythonic way?

我在C中有以下要在Python中模仿的for loop

for (index = 0; index < n; ++index)
{
    for (i = 0, j = index; j < n; ++i, ++j)
    {
         'do something'
    }
}

有没有更优雅的/ Pythonic的方式来做到这一点,或者我必须像这样在循环外声明一个变量:

for index in range(m):
    i = 0
    for j in range(index, m):
        'do something'
        i += 1

很难不知道第二个循环中发生的事情就很难说,但是正如我写的那样,我会说:

for index in range(n):
    for i,j in enumerate(range(index,n)):
        'do something'

将是您这样做的方式。

暂无
暂无

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

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