Unable to create non square matrices


#1

import random
m1= int(input("Write the number of rows for first matrix\n"))
n1 =int(input("Write the number of columns for first matrix\n"))

l = [[random.random() for i in range(m1)]for j in range(n1)]
for i in range(m1):
for j in range(n1):
l[i][j] = input()
for i in range(m1):
for j in range(n1):
print(l[i][j] , end= " ")
print()


#2

Hi @teraninja37943,

Try ...

l = [[random.random() for j in range(n1)]for i in range(m1)]

Now, i handles the rows in the outer loop, and j handles the columns in the inner loop to populate the matrix.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.