如何在 python 中实现给定的 'for' 循环
How to implement the given 'for' loop in python
我在 Python 中实现以下代码(在 Java 中)时遇到困难:
//some code that calculates the values of "r" and "c"
for( int i = r-1 , int j = c-1 , i >= 0 && j >=0 , i-- , j-- ){
// some code
}
我试过 'zip()' 和 'range()' 一起使用,但效果不佳。
如果我们想同时“减少”两个变量但具有不同的值,将如何实现?
试试这个:
for i, j in zip(range(r - 1, -1, -1), range(c - 1, -1, -1)):
print(i, j)
对于每种情况,range()
内有3个参数。第一个表示 from
,第二个参数表示 until
,第三个参数表示 increment/decrement 的多少(在本例中减一)。即range(from, until, increment_or_decrement_by)
.
我在 Python 中实现以下代码(在 Java 中)时遇到困难:
//some code that calculates the values of "r" and "c"
for( int i = r-1 , int j = c-1 , i >= 0 && j >=0 , i-- , j-- ){
// some code
}
我试过 'zip()' 和 'range()' 一起使用,但效果不佳。
如果我们想同时“减少”两个变量但具有不同的值,将如何实现?
试试这个:
for i, j in zip(range(r - 1, -1, -1), range(c - 1, -1, -1)):
print(i, j)
对于每种情况,range()
内有3个参数。第一个表示 from
,第二个参数表示 until
,第三个参数表示 increment/decrement 的多少(在本例中减一)。即range(from, until, increment_or_decrement_by)
.