Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python循环的单行_Python_For Loop_Processing_Nested Loops_Pyprocessing - Fatal编程技术网

Python循环的单行

Python循环的单行,python,for-loop,processing,nested-loops,pyprocessing,Python,For Loop,Processing,Nested Loops,Pyprocessing,我正在使用processing.py 我在听这个图坦卡蒙(爪哇语) 我想知道我是否可以在python中使用相同类型的for循环 for(int y = 0; y < height; y = y + cellSize): for(int x = 0; x < width; x = x + cellSize): rect(x, 0, cellSize, cellSize) 我想可能有一种简单但略有不同的方法可以在python中使用相同类型的嵌套for循环(

我正在使用
processing.py

我在听这个图坦卡蒙(爪哇语)

我想知道我是否可以在python中使用相同类型的for循环

for(int y = 0; y < height; y = y + cellSize):

    for(int x = 0; x < width; x = x + cellSize):

        rect(x, 0, cellSize, cellSize)

我想可能有一种简单但略有不同的方法可以在python中使用相同类型的嵌套for循环(在一行上)

这在python中是等效的。在
范围(0,高度,单元格大小)
中,
0
height
是范围的边界,
cellSize
是计数器外增量的多少

for(int y = 0; y < height; y = y + cellSize):

    for(int x = 0; x < width; x = x + cellSize):

        rect(x, 0, cellSize, cellSize)
for y in range(0, height, cellSize):
    for x in range(0, width, cellSize):
        rect(x, 0, cellSize, cellSize)

您好,欢迎来到SO,
for
循环在python中的工作方式不同。请检查一个基本教程,就像你遗漏了
publicstaticvoidmain
位一样……尽管你很少在python中使用这种构造。而是在容器或生成器上循环。最有可能的情况是,在这里您将拥有一个单元格集合,并且只需对网格中的单元格执行
如何拥有一个单元格集合?没有两个嵌套循环?@KippFhtagn>通过构造对象,使您实际上拥有一个集合对象,或者通过创建一个集合,将循环的方式与循环时的操作分离开来。@nbryans我知道代码应该是等效的,但这并不是构建一个表,只是一个水平的循环行rectangles@KippFhtagn>它应该是
rect(x,y,…
他将您的示例翻译成了正确的python,但您在问题中的循环出现了错误。