NumPy: Print a matrix the way NumPy does

NumPy prints a 2-D array with every number in one width so the columns line up. Reproduce its layout exactly.

What to build

A program that prints an integer matrix in NumPy's print() format.

Requirements

  • Every number is right-aligned to the width of the widest number in the whole matrix and separated by one space.
  • The first row starts with [[, every other row with a space and [; every row ends with ], and the last with ]].
  • A matrix with one row prints as [[...]] on one line.

Input

Line 1: r. Then r lines of whole numbers.

Output

r lines.

Constraints

1 ≤ r ≤ 10; 1 to 10 numbers per row, all rows the same length; |x| ≤ 10^6.

Example

Input:

2
1 2 3
4 5 6

Output:

[[1 2 3]
 [4 5 6]]

Why it works

One common width for the whole array is what makes NumPy's columns line up, and the leading space on later rows aligns them under the first row's inner bracket.

Examples

Example 1 · Input
2
1 2 3
4 5 6
Expected output
[[1 2 3]
 [4 5 6]]
Example 2 · Input
2
1 -20
300 4
Expected output
[[  1 -20]
 [300   4]]

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Find the widest number in the whole matrix; every number is padded to that width.

Hint 2

Rows after the first start with a space, so the brackets line up.

Solution

Show one correct solution (try it yourself first)
r = int(input())
rows = [[int(x) for x in input().split()] for _ in range(r)]
width = max(len(str(v)) for row in rows for v in row)
lines = ["[" + " ".join(str(v).rjust(width) for v in row) + "]" for row in rows]
text = "[" + "\n ".join(lines) + "]"
print(text)

Lesson: Why NumPy →