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

后端 未结 16 1163
醉酒成梦
醉酒成梦 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.

    0 讨论(0)
  • 2020-11-27 07:12

    I used numpy library and it works fine for me. Its just a single line and easy to understand. The input needs to be in a single size separated by space and the reshape converts the list into shape you want. Here (2,2) resizes the list of 4 elements into 2*2 matrix. Be careful in giving equal number of elements in the input corresponding to the dimension of the matrix.

    import numpy as np
    a=np.array(list(map(int,input().strip().split(' ')))).reshape(2,2)
    
    print(a)
    

    Input

    array([[1, 2],
           [3, 4]])
    

    Output

    0 讨论(0)
  • 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'

    0 讨论(0)
  • 2020-11-27 07:20

    you can accept a 2D list in python this way ...

    simply

    arr2d = [[j for j in input().strip()] for i in range(n)] 
    # n is no of rows
    


    for characters

    n = int(input().strip())
    m = int(input().strip())
    a = [[0]*n for _ in range(m)]
    for i in range(n):
        a[i] = list(input().strip())
    print(a)
    

    or

    n = int(input().strip())
    n = int(input().strip())
    a = []
    for i in range(n):
        a[i].append(list(input().strip()))
    print(a)
    

    for numbers

    n = int(input().strip())
    m = int(input().strip())
    a = [[0]*n for _ in range(m)]
    for i in range(n):
        a[i] = [int(j) for j in input().strip().split(" ")]
    print(a)
    

    where n is no of elements in columns while m is no of elements in a row.

    In pythonic way, this will create a list of list

    0 讨论(0)
  • 2020-11-27 07:20

    Apart from the accepted answer, you can also initialise your rows in the following manner - matrix[i] = [0]*n

    Therefore, the following piece of code will work -

    m = int(input('number of rows, m = '))
    n = int(input('number of columns, n = '))
    matrix = []
    # initialize the number of rows
    for i in range(0,m):
        matrix += [0]
    # initialize the matrix
    for i in range (0,m):
        matrix[i] = [0]*n
    for i in range (0,m):
        for j in range (0,n):
            print ('entry in row: ',i+1,' column: ',j+1)
            matrix[i][j] = int(input())
    print (matrix)
    
    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题