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

后端 未结 11 1569
长情又很酷
长情又很酷 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:38

    That way looks pretty straightforward and easy. Are you are saying you want to generalize to multiple layers of loops.... can you give a real-life example?

    Another option I could think of would be to use a function to generate the parameters and then just apply them in a loop

    def generate_params(n):
        return itertools.product(range(n), range(n))
    
    for x,y in generate_params(3):
        do_something()
    

提交回复
热议问题