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

后端 未结 16 1236
醉酒成梦
醉酒成梦 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:11

    row=list(map(int,input().split())) #input no. of row and column
    b=[]
    for i in range(0,row[0]):
        print('value of i: ',i)
        a=list(map(int,input().split()))
        print(a)
        b.append(a)
    print(b)
    print(row)
    

    Output:

    2 3
    
    value of i:0
    1 2 4 5
    [1, 2, 4, 5]
    value of i:  1
    2 4 5 6
    [2, 4, 5, 6]
    [[1, 2, 4, 5], [2, 4, 5, 6]]
    [2, 3]
    

    Note: this code in case of control.it only control no. Of rows but we can enter any number of column we want i.e row[0]=2 so be careful. This is not the code where you can control no of columns.

提交回复
热议问题