print("enter n for nxn matrix:")
n = int(input())
matrix1 = []
matrix2 = []
# elements of first matrix
print("enter elements of first matrix:")
for i in range(0, n):
# elements of first column
print("enter elements of {} column".format(i))
# the elements will be separated by a space
# input().split() will split the string
# '1 23 456'.split() will give ['1', '23', '456']
# map will convert its elements into integers [1, 23, 456]
matrix1.append(map(int,input().split()))
print("Matrix 1 is {}".format(matrix1))
# taking elements of second matrix
print("enter elements of second matrix")
for i in range(0, n):
print("enter elements of {} column".format(i))
matrix2.append(map(int,input().split()))
print("Matrix 2 is {}".format(matrix2))
# addition
addition = []
for i in range(0, n):
a = []
for j in range(0, n):
# making a addition matrix's column to append
# making a 1D matrix with elements as sum of elements of respective columns of both matrices
a.append(matrix1[i][j] + matrix2[i][j])
addition.append(a)
print("Addition of matrix is {}".format(addition))
# multiplication
multiplication = []
for i in range(0, n):
a = []
for j in range(0, n):
b = 0
for k in range(0, n):
b = b + (matrix1[i][k] * matrix2[k][j])
a.append(b)
multiplication.append(a)
print("Multiplication of matrix is {}".format(multiplication))
When you use the map
function it return you the map object
map(int,"3".split())
<map at 0x7f3550af16d8>
If you want to push the list you should call it for example:
list(map(int,"3".split()))
[3]
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments