How ro read mutli lined input into 2d arrays in python

前端 未结 2 442
野的像风
野的像风 2021-01-28 22:43

I\'ve been across a problem in python, the input format of the 2d array to be read is

3           # number of rows and columns of a square matrix
1 2 3       #          


        
2条回答
  •  伪装坚强ぢ
    2021-01-28 23:03

    If the numbers are separated by a single space your can use the following code:

    n = int(input())
    matrix = []
    for i in range(n):
      row = [int(x) for x in input().split()]
      matrix.append(row)
    

    If you have different separator then space you can put as argument in the split() function.

提交回复
热议问题