0

I want to make my own matrix solution code. I having problem when taking values. my code is below:

ask_r = int(raw_input("How many rows: "))
ask_c = int(raw_input("How many columns: "))

"""
[1,2,3]
[3,4,5]
[7,8,9]
"""

"""

matrix = [[1,2,3],[4,5,6],[7,8,9]]

"""
col_m = [0]*ask_c
row_m = [col_m]*ask_r

for i in range(ask_r):
    for j in range(ask_c):
        val = input("Put in: ")
        row_m[i][j] = val
print row_m

What's wrong there as the input changes the value of all the three inner lists(considered as columns).

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504

1 Answers1

0

row_m = [col_m]*ask_r creates a list of ask_r times the same reference to col_m.

You are looking for something like

row_m = [[0]*ask_c for _ in range(ask_r)]
Hyperboreus
  • 31,997
  • 9
  • 47
  • 87