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

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

    If you want to take n lines of input where each line contains m space separated integers like:

    1 2 3
    4 5 6 
    7 8 9 
    

    Then you can use:

    a=[] // declaration 
    for i in range(0,n):   //where n is the no. of lines you want 
     a.append([int(j) for j in input().split()])  // for taking m space separated integers as input
    

    Then print whatever you want like for the above input:

    print(a[1][1]) 
    

    O/P would be 5 for 0 based indexing

提交回复
热议问题