Single line nested in loops

Wrote this function in python, which transfers the matrix:

def transpose(m): height = len(m) width = len(m[0]) return [ [ m[i][j] for i in range(0, height) ] for j in range(0, width) ] 

In this process, I realized that I did not understand how a single line nested for loops is executed. Please help me understand by answering the following questions:

  • What is the order of execution of this for loop?
  • If I had a triple nested loop, what order would it follow?
  • What will be equal to an equal lifeless cycle?

Considering,

 [ function(i,j) for i,j in object ] 
  • What type must be an object in order to use it for loop structure?
  • What is the order in which i and j are assigned to elements in an object?
  • Can it be modeled by a different loop structure?
  • Could this for a loop be nested with a similar or different structure for the loop? And what will it look like?

Additional information is also appreciated.

+49
python loops for-loop nested-loops nested
Jun 09 '13 at 5:08 on
source share
3 answers

The best source of information is the official official Python list recognition tutorial . List mappings are almost the same as for loops (of course, any understanding of the list can be written as a for loop), but they are often faster than using a for loop.

Take a look at this longer list comprehension from the tutorial (the if part filters the comprehension, only those parts that pass the if statement are passed to the final part of the list comprehension (here (x,y) ):

 >>> [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y] [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)] 

This is exactly the same as this nested loop loop (and, as the tutorial says, notice how the for and if order are the same).

 >>> combs = [] >>> for x in [1,2,3]: ... for y in [3,1,4]: ... if x != y: ... combs.append((x, y)) ... >>> combs [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)] 

The main difference between understanding a list and a for loop is that the end of the for loop (where you do something) happens at the beginning, not the end.

To your questions:

What type must be an object to use this for loop structure?

An iterable . Any object that can generate a (finite) set of elements. These include any container, lists, sets, generators, etc.

What is the order in which i and j are assigned to elements in an object?

They are assigned exactly in the order in which they are generated from each list, as if they were in a nested loop (for your first understanding, you get 1 element for i, then each value from j, the 2nd element in i, then each value from j, etc.)

Is it possible to simulate a different loop structure?

Yes, already shown above.

Could this for a loop be nested with a similar or different structure for the loop? And what would it look like?

Of course, but this is not a great idea. Here, for example, you get a list of character lists:

 [[ch for ch in word] for word in ("apple", "banana", "pear", "the", "hello")] 
+89
Jun 09 '13 at 5:26
source share

You might be interested in itertools.product , which returns iterable yields of values ​​from all iterations it repeats. That is, itertools.product(A, B) gives all the values ​​of the form (a, b) , where the values ​​of a are taken from a , and the values ​​of b are taken from b . For example:

 import itertools A = [50, 60, 70] B = [0.1, 0.2, 0.3, 0.4] print [a + b for a, b in itertools.product(A, B)] 

Fingerprints:

 [50.1, 50.2, 50.3, 50.4, 60.1, 60.2, 60.3, 60.4, 70.1, 70.2, 70.3, 70.4] 

Notice how the last argument passed to itertools.product is "internal." As a rule, itertools.product(a 0 , a 1 , ... a n ) is equal to [(i 0 , i 1 , ... i n ) for i n in a n for i n-1 in a n-1 ... for i 0 in a 0 ]

+18
Jun 09 '13 at 10:13 on
source share

First of all, your first code does not use a for per se loop, but a list .

  • It would be equivalent

    for j in the range (0, width): for i in the range (0, height): m [I] [J]

  • In the same way, as a rule, it is located as a loop, from right to left. But the syntax for understanding a list is more complicated.

  • I'm not sure this question asks




  • Any iterative object that gives iterative objects that give exactly two objects (which will have a mouth - ie [(1,2),'ab'] )

  • The order in which the object gives iteration. i goes to the first output, j second.

  • Yes, but not so pretty. I believe this is functionally equivalent to:

     l = list ()
     for i, j in object:
         l.append (function (i, j))
    

    or even better to use map :

     map(function, object) 

    But, of course, the function would have to get i , j .

  • Isn't that the same question as 3?

+3
Jun 09 '13 at 5:30
source share



All Articles