How to input matrix (2D list) in Python?
Solution 1
The problem is on the initialization step.
for i in range (0,m):
matrix[i] = columns
This code actually makes every row of your matrix
refer to the same columns
object. If any item in any column changes - every other column will change:
>>> for i in range (0,m):
... matrix[i] = columns
...
>>> matrix
[[0, 0, 0], [0, 0, 0]]
>>> matrix[1][1] = 2
>>> matrix
[[0, 2, 0], [0, 2, 0]]
You can initialize your matrix in a nested loop, like this:
matrix = []
for i in range(0,m):
matrix.append([])
for j in range(0,n):
matrix[i].append(0)
or, in a one-liner by using list comprehension:
matrix = [[0 for j in range(n)] for i in range(m)]
or:
matrix = [x[:] for x in [[0]*n]*m]
See also:
Hope that helps.
Solution 2
you can accept a 2D list in python this way ...
simply
arr2d = [[j for j in input().strip()] for i in range(n)]
# n is no of rows
for characters
n = int(input().strip())
m = int(input().strip())
a = [[0]*n for _ in range(m)]
for i in range(n):
a[i] = list(input().strip())
print(a)
or
n = int(input().strip())
n = int(input().strip())
a = []
for i in range(n):
a[i].append(list(input().strip()))
print(a)
for numbers
n = int(input().strip())
m = int(input().strip())
a = [[0]*n for _ in range(m)]
for i in range(n):
a[i] = [int(j) for j in input().strip().split(" ")]
print(a)
where n is no of elements in columns while m is no of elements in a row.
In pythonic way, this will create a list of list
Solution 3
If the input is formatted like this,
1 2 3
4 5 6
7 8 9
a one liner can be used
mat = [list(map(int,input().split())) for i in range(row)]
explanation with example:
-
input()
takes a string as input. "1 2 3" -
split()
splits the string by whitespaces and returns a
list of strings. ["1", "2", "3"] -
list(map(int, ...))
transforms/maps the list of strings into a list of ints. [1, 2, 3] - All these steps are done row times and these lists are stored in another list.
[[1, 2, 3], [4, 5, 6], [7, 8, 9]], row = 3
Solution 4
If you want to take n lines of input where each line contains m space separated integers like:
1 2 3
4 5 6
7 8 9
Then you can use:
a=[] // declaration
for i in range(0,n): //where n is the no. of lines you want
a.append([int(j) for j in input().split()]) // for taking m space separated integers as input
Then print whatever you want like for the above input:
print(a[1][1])
O/P would be 5 for 0 based indexing
Solution 5
Apart from the accepted answer, you can also initialise your rows in the following manner -
matrix[i] = [0]*n
Therefore, the following piece of code will work -
m = int(input('number of rows, m = '))
n = int(input('number of columns, n = '))
matrix = []
# initialize the number of rows
for i in range(0,m):
matrix += [0]
# initialize the matrix
for i in range (0,m):
matrix[i] = [0]*n
for i in range (0,m):
for j in range (0,n):
print ('entry in row: ',i+1,' column: ',j+1)
matrix[i][j] = int(input())
print (matrix)
Iqazra
Updated on June 30, 2020Comments
-
Iqazra almost 4 years
I tried to create this code to input an m by n matrix. I intended to input
[[1,2,3],[4,5,6]]
but the code yields[[4,5,6],[4,5,6]
. Same things happen when I input other m by n matrix, the code yields an m by n matrix whose rows are identical.Perhaps you can help me to find what is wrong with my code.
m = int(input('number of rows, m = ')) n = int(input('number of columns, n = ')) matrix = []; columns = [] # initialize the number of rows for i in range(0,m): matrix += [0] # initialize the number of columns for j in range (0,n): columns += [0] # initialize the matrix for i in range (0,m): matrix[i] = columns for i in range (0,m): for j in range (0,n): print ('entry in row: ',i+1,' column: ',j+1) matrix[i][j] = int(input()) print (matrix)
-
executable over 5 yearsCan you explain why this should answer OP question ?
-
Taufiq Ahommed Neloy about 2 yearswould you please give me any reference link or explain how this statement work??
-
Julkar9 about 2 years@TaufiqAhommedNeloy I have added an explanation.
-
Taufiq Ahommed Neloy about 2 yearsthank you so much for the explanation @Julkar9