How to print matrix in Python
A matrix is a data structure where elements are arranged in rows and columns. There is no built-in type for matrices in Python. Usually a list of a list is used for operating on matrix.
Numpy is a Python library which provides support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions.
In order to work with NumPy, you have to install it in your system.
After that, we can import the numpy library and use it.
We can use numpy.array()
to print the matrix.
numpy.array(object)
is used to get a NumPy array
containing each row and column of the two-dimensional list object
.
import numpy li = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] mat = numpy.array(li) print(mat)
Output:
[[1 2 3] [4 5 6] [7 8 9]]
Subscribe
Login
Please login to comment
0 Discussion