Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to input matrix (2D list) in Python?

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)
like image 555
Iqazra Avatar asked Mar 30 '14 06:03

Iqazra


People also ask

How do you add to a 2D list in python?

Append an element to a 2D list. Use the list indexing syntax a_2d_list[x] to get the nested list at index x in a 2D list. Call list. append(object) with this nested list as list and the desired element as object to append an element to the nested list.

How do you reference a 2D list in python?

Use list indexing to access elements in a 2D list. Use the list indexing syntax a_2d_list[x][y] to access an element at index y in the nested list at index x .

How to input a matrix in Python?

As you know in python everything is dynamic (changes in size of matrix take place at run time), there is no hard and fast rule to input matrix. As you matrix is basically another form of 2D list and an easy way to give you an understanding (easy way).

How to get data from a 2-D list in Python?

For each row in the 2-d list, initialize the row by using ar.append ([]) like you did ar [i] = new int [n]; in C/C++. Then, get your data by using input and append it to ar [i].

How to take n lines of input from a 2D list?

you can accept a 2D list in python this way ... where n is no of elements in columns while m is no of elements in a row. If you want to take n lines of input where each line contains m space separated integers like: Apart from the accepted answer, you can also initialise your rows in the following manner - matrix [i] = [0]*n

How to decalre a two dimensional list in Python?

You can decalre a two dimensional list in this manner initialized to a certain value. Actually, you don't need the column count. Just enter the number of rows and give your input space separated. At Python-3 we can easily take row and column input side by side as well as the data like a matrix by this process.


1 Answers

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:

  • How to initialize a two-dimensional array in Python?

Hope that helps.

like image 182
alecxe Avatar answered Sep 20 '22 11:09

alecxe