社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

如何在python中为相应的java语法编写同步for循环

Aadi • 3 年前 • 1227 次点击  

如何用python编写这个同步for循环?

Java语法:

for (int l=0; l<n; l++) {
  for(int x=0, y=x+l; x<n && y<n; x++, y++) {

  }
}

我在Python 3中尝试过这个。x:

for x, y in zip(range(n), range(x+l,n))

但我错了 local variable x referenced before assignment .

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/129170
 
1227 次点击  
文章 [ 1 ]  |  最新文章 3 年前
Selcuk
Reply   •   1 楼
Selcuk    3 年前

Python中没有确切的对应项,但这应该是等效的(而且我更容易理解)。注意 x<n 条件在Java版本中是多余的:

for l in range(n):
    for y in range(l, n):
        x = y - l
        # The rest of your code