List to Matrix In python by R4R Team

Example-

list is l=[1,2,3]
matrix is [1 2 3]

list is [[1,2],[3,4]]
matrix is
[[1 2]
[3 4]]

Syntax-

mat=array(list)


program-

#import numpy
import numpy as np

l=[1,2,3,4]
arr=np.array(l)
print("Matrix isn",arr)

l=[[1,2],[3,4]]
arr=np.array(l)
print("nMatrix isn",arr)

l=[[1,2,3],[3,2,1],[1,0,1]]
arr=np.array(l)
print("nMatrix isn",arr)


output-

Matrix is
[1 2 3 4]

Matrix is
[[1 2]
[3 4]]

Matrix is
[[1 2 3]
[3 2 1]
[1 0 1]]

-In this program, we have a different list, some are simple list, and some are nested list.
-So by Numpy we convert that list into the array so this array is work as a matrix.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!