Python Practice Lab
mediumUnit 9 · Comprehensions: list, set, dict

P27. Matrix Transpose

Comprehensions: list, set, dict

Read a matrix with r rows and c columns. Use a nested list comprehension to build its transpose (rows become columns). Also use list comprehensions to find the sum of each row and of each column of the original matrix.

Input Format

  • Line 1: r c
  • Next r lines: c integers separated by spaces

Output Format

  • c lines: the rows of the transpose, numbers separated by single spaces
  • Next line: Row sums: <list>
  • Last line: Column sums: <list>

Constraints

  • 1 ≤ r, c ≤ 10
  • -100 ≤ each value ≤ 100

Sample Test Cases

Sample 1
Input
2 3
1 2 3
4 5 6
Output
1 4
2 5
3 6
Row sums: [6, 15]
Column sums: [5, 7, 9]
Sample 2
Input
1 1
7
Output
7
Row sums: [7]
Column sums: [7]

Submitting checks 2 sample cases and 2 hidden test cases.

Hints

Saved
Custom 1

This is your own input, so Run will show what your program prints but can't say whether it's correct.