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

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

    When faced with that sort of program logic, I would probably break up the sequence of loops into two or more separate functions.

    Another technique in Python is to use list comprehensions where possible, instead of a loop.

    0 讨论(0)
  • 2020-12-04 18:39

    Have you looked into List Comprehensions?

    Something like:

    [do_something() for x in range(3) for y in range(3)]
    
    0 讨论(0)
  • 2020-12-04 18:39

    you can also use the map() function

    0 讨论(0)
  • 2020-12-04 18:40

    My personal argument would be that you're likely doing something wrong if you have 6 nested loops...

    That said, functional decomposition is what you're looking for. Refactor so some of the loops happen in seperate function calls, then call those functions.

    0 讨论(0)
  • 2020-12-04 18:41

    Python iterators, and generators in particular, exist exactly to allow the nice refactoring of otherwise-complicated loops. Of course, it's hard to get an abstraction out from a simple example, but assuming the 3 needs to be a parameter (maybe the whole range(3) should be?), and the two functions you're calling need some parameters that are loop variables, you could refactor the code:

      for y in range(3):
        for x in range(3):
          do_something(x, y)
          for y1 in range(3):
            for x1 in range(3):
              do_something_else(x, y, x1, y1)
    

    into, e.g.:

    def nestloop(n, *funcs):
      head = funcs[0]
      tail = funcs[1:]
      for y in range(n):
        for x in range(n):
          yield head, x, y
          if tail:
            for subtup in nestloop(n, *tail):
               yield subtup[:1] + (x, y) + subtup[1:]
    
    for funcandargs in nestloop(3, do_something, do_something_else):
      funcandargs[0](*funcandargs[1:])
    

    The exact kind of refactoring will no doubt need to be tweaked for your exact purposes, but the general point that iterators (and usually in fact just simple generators) afford very nice refactorings of loops remains -- all the looping logic goes inside the generator, and the application-level code is left with simple for loops and actual application-relevant processing of the items yielded in the for loops.

    0 讨论(0)
提交回复
热议问题