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

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

    a = []
    b = []
    
    m=input("enter no of rows: ")
    n=input("enter no of coloumns: ")
    
    for i in range(n):
         a = []
         for j in range(m):
             a.append(input())
         b.append(a)
    

    Input : 1 2 3 4 5 6 7 8 9

    Output : [ ['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9'] ]

提交回复
热议问题