python - Pythonic shortcut for doubly nested for loops? -


Consider that I have a function that takes the Tupply Logic (X, Y), where X was in the range (X ), And in the y category (y), the usual way to do this:

  For the class X (x): In range Y: function (x, y)   

To do something

  x_like_range for some (x, y): function (xy)   

Was Tuplal (X, Y)?

You can use

  & gt; & Gt; & Gt; Import Products from Iteratolls & gt; & Gt; & Gt; & Gt; & Gt; & Gt; For x, y product (category (3), category (4)): ... print (x, y) ... (0, 0) (0, 1) (0, 2) (0, 3) (1, 0) (1, 1) (1, 2) (1, 3) ... and so on   

Your code will look like this:

 < Code> Product X (Range (X), Range (Y)): Function (x, y)    

Comments