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

后端 未结 16 1250
醉酒成梦
醉酒成梦 2020-11-27 06:34

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 happ

16条回答
  •  情歌与酒
    2020-11-27 07:06

    This code takes number of row and column from user then takes elements and displays as a matrix.

    m = int(input('number of rows, m : '))
    n = int(input('number of columns, n : '))
    a=[]
    for i in range(1,m+1):
      b = []
      print("{0} Row".format(i))
      for j in range(1,n+1):
        b.append(int(input("{0} Column: " .format(j))))
      a.append(b)
    print(a)
    

提交回复
热议问题