I couldn’t quite understand how this code works

Asked

Viewed 45 times

1

I saw this code to transpose the matrix but I couldn’t quite understand how it works.

For me this row[i] looks more like a "marker" of an index, for example, line 1, line 2, line 3 and line 4. I don’t quite understand how it works.

In addition to the range(2) also confuses me because for me range was used for a range of numbers.

Can someone explain to me?

matrix = [[1, 2],[3,4],[5,6],[7,8]]
transpose = [[row[i] for row in matrix] for i in range(2)]
print(transpose)

1 answer

2


If so, it makes it easier to understand?

matrix = [[1, 2], [3, 4], [5, 6], [7, 8]]
transpose = []
for i in range(2):
    linha = []
    for row in matrix:
        linha.append(row[i])

    transpose.append(linha)

This is equivalent to what the comprehensilist on is making.


In addition to the range(2) also confuses me because for me range was used for a range of numbers.

Yes, range is what you do with these numbers.

Generally, range(n) represents the interval between zero and n - 1 (the n is not included in range). So range(2) represents the range between zero and 1 (that is, only the numbers 0 and 1).

Then the for i in range(2) is serving to traverse the numbers 0 and 1, and then, for each "line" of the matrix, we take the element that is in the position i. In other words:

  • in the first iteration, i valley zero.
  • each element of matrix is a list (the first element is the list [1, 2], the second is the list [3, 4], etc). The for row in matrix scroll through those lists
    • within the for row in matrix, take the first element from each list (row[i], and how i is worth zero, you get the row[0], which is the first item on the list)
    • that is, this for adds the 1, then the 3, then the 5 and finally the 7, all on the same list
    • then this list [1, 3, 5, 7] is added in transpose
  • in the second iteration, i is worth 1, and now linha will be a list with the second element of each sub-list of matrix

In this case, I found "wrong" the use of range(2), because the code only works if all sublists of matrix have exactly two elements (if more, they would be ignored). A little more pythonic, without depending on a fixed value, it would be:

matrix = [[1, 2], [3, 4], [5, 6], [7, 8]]
transpose = []
for row in zip(*matrix):
    transpose.append(list(row))

print(transpose) # [[1, 3, 5, 7], [2, 4, 6, 8]]

zip serves to scroll through multiple lists at once, returning tuples containing the elements of each of them. By passing *matrix, with the asterisk, I’m doing the unpacking, that is, it is as if I pass each of the sub-lists as an argument to zip.

With this, in the first iteration of for, the variable row will be a tuple containing the first element of each of the sub-lists. In the second iteration, it will be a tuple containing the second element of each and so on.

Then, inside the loop, I turn this tuple into a list and add in transpose.

If you want, you can also use a comprehensilist on:

transpose = [ list(row) for row in zip(*matrix) ]

Of course you can improve more, because the code does not check if all the sub-lists have the same size, etc (i.e., it assumes that the matrix is correctly filled). But the basic idea is this.

  • 1

    Thank you very much, it helped a lot!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.