Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
The matrix is m = [[1, 2], [3, 4], [5, 6]], three rows of two. Write comprehensions for (1) the list of the sums of each row, (2) the flat list of all the numbers, and (3) the transposed matrix, two rows of three.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Plan.
[sum(row) for row in m].[x for row in m for x in row].i of a row, collect the i-th item of every row: [[row[i] for row in m] for i in range(2)].Check. The row sums are 3, 7 and 11. The flat list is 1 to 6. The transpose takes the first items (1, 3, 5) as the first row, and the second items (2, 4, 6) as the second row.
Answer: [3, 7, 11], [1, 2, 3, 4, 5, 6] and [[1, 3, 5], [2, 4, 6]].
Log in to join the discussion.
Loading…