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

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

    If your matrix is given in row manner like below, where size is s*s here s=5 5 31 100 65 12 18 10 13 47 157 6 100 113 174 11 33 88 124 41 20 140 99 32 111 41 20

    then you can use this

    s=int(input())
    b=list(map(int,input().split()))
    arr=[[b[j+s*i] for j in range(s)]for i in range(s)]
    

    your matrix will be 'arr'

提交回复
热议问题