0

I have an experience in java and c++ for 7 years now. I recently started learning python. Can someone please help me on how to read the input for the matrix and display the same in matrix format. This is the code I wrote:

import sys
# no of rows are equal to the number of columns.
n = int(input("Enter the number of rows in a matrix"))
a = [[0 for x in range (n)] for y in range(n)]
for i in range (n):
 for j in range(n):
 a[i][j]=int(input())
 print (a[i][j])
 print("\n")
asked Nov 6, 2017 at 21:16
1

8 Answers 8

1

If you use pandas, and make a dataframe... if you enter 4 for the number of rows, and the numbers 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16... the script below

import pandas as pd
n = int(input("Enter the number of rows in a matrix: "))
a = [[0] * n for i in range(n)]
col_names = []
row_names = []
for i in range(n):
 col_names.append('col ' + str(i+1))
 row_names.append('row ' + str(i+1)) 
 for j in range(n):
 a[i][j] = int(input())
print(pd.DataFrame(a,columns = col_names, index = row_names))

...will produce the following output.

 col 1 col 2 col 3 col 4
row 1 1 2 3 4
row 2 5 6 7 8
row 3 9 10 11 12
row 4 13 14 15 16

Another option... using numpy... and the same values from the first option...

import numpy as np
n = int(input("Enter the number of rows in a matrix: "))
a = [[0] * n for i in range(n)]
for i in range(n):
 for j in range(n):
 a[i][j] = int(input())
print(np.matrix(a))

...would produce

[[ 1 2 3 4]
 [ 5 6 7 8]
 [ 9 10 11 12]
 [13 14 15 16]]
answered Nov 6, 2017 at 22:07
Sign up to request clarification or add additional context in comments.

Comments

1
 #program to print matirx by row and column input
a,b = map(int,input("Enter row and column of matrix separated by a space ").split())
# empty list for matrix 
m=[] 
# taking matrix input
for i in range(1,a+1):
 l=[]
 for j in range(1,b+1):
 n=int(input(f"Enter a[{i}][{j}] element "))
 l.append(n)
 m.append(l)
#printing in matrix form
for i in range(a):
 for j in range(b):
 if(j==b-1):
 print(m[i][j], end="")
 else:
 print(m[i][j], end=" ")
 if i!=a-1:
 print()
answered Oct 27, 2018 at 12:45

Comments

0

This example from a program I use for calculating bigger matrices:

matrix = []
rows = int(input("Num rows: "))
cols = int(input("Num columns: "))
for r in range(rows):
 row = []
 for c in range(cols):
 row.append(int(input("M1-> R: {} C: {}\n>>>".format(r+1, c+1))))
 matrix.append(row)
print(matrix)

This will work for non-square matrices as well. I also use Sympy for doing matrix calculations, but that's besides the point:)

answered Nov 6, 2017 at 23:00

Comments

0
print"ENter r and c"
r = int(input("Enter the number of rows in a matrix"))
c = int(input("Enter the number of columns in a matrix"))
a = [[int(input()) for x in range (c)] for y in range(r)]
Stephan Vierkant
10.3k8 gold badges67 silver badges102 bronze badges
answered Jul 20, 2018 at 9:42

Comments

0
import numpy as np
#Taking the number of rows and columns from user
n=int(input("Enter the number of Rows\n"))
m=int(input("Enter the number of Columns\n"))
"""
 You can either use this loop method for below list comprehension;
a=[]
for i in range(n):
 a.append([0] * m )
"""
#Creating a Empty matrix as as per the instruction of user;
a = [ [0] * m for i in range(n) ]
#Taking the element for a matrix from user;
for i in range (n):
 for j in range(m):
 print("Enter Element No:",i,j)
 a[i][j] = int(input())
print(np.matrix(a))
PopHip
7687 silver badges24 bronze badges
answered Jun 14, 2019 at 7:42

Comments

0
r = int(input("Enter Number of Rows : "))
c = int(input("Enter Number of Cols : "))
a = []
for i in range(r):
 b = []
 for j in range(c):
 j = int(input("Enter Number in pocket [" + str(i) + "][" + str(j) + "]"))
 b.append(j)
 a.append(b)
print("Matrix is ......")
for i in range(r):
 for j in range(c):
 print(a[i][j], end=" ")
 print()
Bogdan Doicin
2,4345 gold badges31 silver badges38 bronze badges
answered Apr 27, 2020 at 10:45

Comments

0
r = int(input("Enter the number of rows in a matrix"))
c = int(input("Enter the number of columns in a matrix")) 
a = [list(map(int,input().split())) for _ in range(r)]
for i in a:
 print(*i)

Comments

0
list = []
for i in range(3):
 list.append([])
 for j in range(3):
 list[i].append(2)
 
for i in range(3):
 for j in range(3):
 print(list[i][j],end=" ")
 print()
4b0
22.4k30 gold badges97 silver badges143 bronze badges
answered Jan 4, 2021 at 14:09

1 Comment

It's good practice on StackOverflow to add an explanation as to why your solution should work.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.