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

后端 未结 11 1577
长情又很酷
长情又很酷 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-04 18:25

    From your code it looks like you want to perform an operation with every possible pair of points where x and y are in the range 0..2.

    To do that:

    for x1,y1,x2,y2 in itertools.product(range(3), repeat=4):
        do_something_with_two_points(x1,y1,2,y2)
    

    The operation do_something_with_two_points will be called 81 times - once for every possible combination of points.

提交回复
热议问题