In python is there an easier way to write 6 nested for loops?

后端 未结 11 1570
长情又很酷
长情又很酷 2020-12-04 17:49

This problem has been getting at me for a while now. Is there an easier way to write nested for loops in python? For example if my code went something like this

11条回答
  •  半阙折子戏
    2020-12-04 18:22

    This is fairly common when looping over multidimensional spaces. My solution is:

    xy_grid = [(x, y) for x in range(3) for y in range(3)]
    
    for x, y in xy_grid:
        # do something
        for x1, y1 in xy_grid:
            # do something else
    

提交回复
热议问题