Transpose and Flatten

Sort by

recency

|

330 Discussions

|

  • + 0 comments
    import numpy
    
    arr = []
    n, m = list(map(int, input().split()))
    
    for _ in range(n):
        arr.append(list(map(int, input().split())))
    
    np_arr = numpy.array(arr)
    print(np_arr.T)
    print(np_arr.flatten())
    
  • + 0 comments

    import numpy

    n,m=map(int,input().split()) matrix=[] for i in range(n): matrix.append(list(map(int, input().split())))

    array=numpy.array(matrix) print(numpy.transpose(array)) print(array.flatten())

  • + 0 comments

    With the help of regEx :

    import sys, re, numpy
    
    
    entry = sys.stdin.read().replace(' ', '').splitlines()[1:]
    
    my_array = []
    for i in range(len(entry)):
       my_array.append([int(i) for i in re.findall(r"\d", entry[i])])
    
    my_array = numpy.array(my_array)
    print(numpy.transpose(my_array), my_array.flatten(), sep='\n')
    
  • + 0 comments
    import numpy as np
    
    # Input number of rows and columns
    rows, cols = map(int, input().split())
    
    data = []
    for _ in range(rows):
        row = list(map(int, input().split()))
        data.append(row)
    
    # Convert the list of lists to a NumPy array
    array = np.array(data)
    print(np.transpose(array))
    print(array.flatten())
    
  • [deleted]Challenge Author
    + 0 comments

    For Python3

    import numpy
    
    n, m = map(int, input().split())
    lst = []
    
    for i in range(n):
        lst.append([*map(int, input().split())])
    
    arr = numpy.array(lst)
    print(numpy.transpose(arr))
    print(arr.flatten())