Hướng dẫn how do you sum a row in an array in python? - làm thế nào để bạn tính tổng một hàng trong một mảng trong python?

Làm việc trên một dự án cung cấp cho chúng tôi triều đại miễn phí về những gì sẽ sử dụng. Vì vậy, tôi quyết định tôi sẽ học Python cho nó.

Show

    Để làm cho điều này ngắn gọn, tôi muốn tổng tất cả các yếu tố trong một "hàng" của một ma trận tôi đang đọc.

    Đây là mảng 2D của tôi trông như thế nào sau khi tôi đọc trong bảng từ tệp văn bản của tôi.

    ['0000' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '1']
    ['0001' '0' '1' '0' '1' '0' '0' '0' '0' '1' '0' '0' '0' '0' '1']
    ['0010' '0' '1' '0' '1' '0' '0' '0' '0' '1' '0' '0' '0' '0' '1']
    ['0011' '0' '1' '0' '1' '0' '0' '0' '0' '1' '0' '0' '0' '0' '1']
    ['0100' '0' '0' '0' '0' '0' '1' '0' '1' '0' '0' '1' '0' '0' '1']
    ['0101' '0' '0' '1' '0' '0' '0' '1' '0' '0' '1' '0' '1' '1' '0']
    ['0110' '0' '0' '1' '0' '1' '0' '0' '0' '0' '1' '0' '1' '1' '0']
    ['0111' '0' '0' '1' '0' '0' '0' '0' '0' '0' '1' '0' '0' '1' '0']
    ['1000' '0' '0' '0' '0' '0' '1' '0' '1' '0' '0' '1' '0' '0' '1']
    ['1001' '1' '0' '0' '0' '0' '0' '1' '0' '0' '1' '0' '1' '1' '0']
    ['1010' '1' '0' '0' '0' '1' '0' '0' '0' '0' '1' '0' '1' '1' '0']
    ['1011' '1' '0' '0' '0' '0' '0' '0' '0' '0' '1' '0' '0' '1' '0']
    ['1100' '0' '0' '0' '0' '0' '1' '0' '1' '0' '0' '1' '0' '0' '1']
    ['1101' '0' '0' '0' '0' '0' '0' '1' '0' '0' '0' '0' '1' '1' '0']
    ['1110' '0' '0' '0' '0' '1' '0' '0' '0' '0' '0' '0' '1' '1' '0']
    ['1111' '0' '0' '0' '0' '0' '0' '0' '0' '0' '1' '0' '1' '1' '0']
    

    Tôi muốn tổng hợp tất cả các phần tử của mỗi hàng không bao gồm chỉ mục 0 (số 4 chữ số). Và sau đó lưu trữ những khoản tiền đó trong một danh sách.

    Đây là danh sách các khoản tiền của tôi sẽ trông như thế nào:

    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    

    Tuy nhiên, đây là những gì mã đầu ra của tôi:

    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    

    Tôi không chắc lỗi là gì, nhưng tôi nghĩ nó phải làm với chuyển đổi chuỗi/int. Vì bảng của tôi ở trong chuỗi, nhưng tôi chuyển đổi nó thành INTS để tổng hợp. Gỡ lỗi nó hiển thị kết quả chính xác, vì vậy tôi không chắc lỗi ở đâu.

    Đây là mã của tôi:

    import numpy
    
    print ("Reading..")
    txtfile = open("test1.txt", "r")
    print(txtfile.readline())
    txtfile.close()
    
    r= numpy.genfromtxt('test1.txt',dtype=str,skiprows=1)
    
    for x in range (0,len(r)):
        print(r[x])
    
    allTested = [0] * (len(r[0]) - 1)
    num1s = [0] * (len(r[0]) - 1)
    
    print("number of rows:", len(r))
    print("number of cols:", len(r[0]))
    print("num1s before:",num1s)
    
    for x in range (0,len(r)):
        for y in range(1,len(r[0])):
            num1s[y-1] += int(r[x][y])
    
    
    print("num1s after :",num1s)
    

    Cải thiện bài viết

    Lưu bài viết

  • Đọc
  • Bàn luận
  • Cải thiện bài viết

    Lưu bài viết

    Đọc This function returns the sum of array elements over the specified axis.

    Bàn luận 
    arr : input array. 
    axis : axis along which we want to calculate the sum value. Otherwise, it will consider arr to be flattened(works on all the axis). axis = 0 means along the column and axis = 1 means working along the row. 
    out : Different array in which we want to place the result. The array must have same dimensions as expected output. Default is None. 
    initial : [scalar, optional] Starting value of the sum. 
    Return : Sum of the array elements (a scalar value if axis is none) or array with sum values along the specified axis.

    numpy.sum (mảng, trục, dtype, out): hàm này trả về tổng các phần tử mảng trên trục được chỉ định. 

    Python3

    import numpy as np

    arr ____10 import8import9

    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    3numpy as np1
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    3numpy as np3
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    3numpy as np5
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    3______8788

    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    2
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    3
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    4
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    5

    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    2
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    7
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    8
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    9
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    4
    import numpy
    
    print ("Reading..")
    txtfile = open("test1.txt", "r")
    print(txtfile.readline())
    txtfile.close()
    
    r= numpy.genfromtxt('test1.txt',dtype=str,skiprows=1)
    
    for x in range (0,len(r)):
        print(r[x])
    
    allTested = [0] * (len(r[0]) - 1)
    num1s = [0] * (len(r[0]) - 1)
    
    print("number of rows:", len(r))
    print("number of cols:", len(r[0]))
    print("num1s before:",num1s)
    
    for x in range (0,len(r)):
        for y in range(1,len(r[0])):
            num1s[y-1] += int(r[x][y])
    
    
    print("num1s after :",num1s)
    
    1
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    0
    import numpy
    
    print ("Reading..")
    txtfile = open("test1.txt", "r")
    print(txtfile.readline())
    txtfile.close()
    
    r= numpy.genfromtxt('test1.txt',dtype=str,skiprows=1)
    
    for x in range (0,len(r)):
        print(r[x])
    
    allTested = [0] * (len(r[0]) - 1)
    num1s = [0] * (len(r[0]) - 1)
    
    print("number of rows:", len(r))
    print("number of cols:", len(r[0]))
    print("num1s before:",num1s)
    
    for x in range (0,len(r)):
        for y in range(1,len(r[0])):
            num1s[y-1] += int(r[x][y])
    
    
    print("num1s after :",num1s)
    
    3

    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    2
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    7
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    8
    import numpy
    
    print ("Reading..")
    txtfile = open("test1.txt", "r")
    print(txtfile.readline())
    txtfile.close()
    
    r= numpy.genfromtxt('test1.txt',dtype=str,skiprows=1)
    
    for x in range (0,len(r)):
        print(r[x])
    
    allTested = [0] * (len(r[0]) - 1)
    num1s = [0] * (len(r[0]) - 1)
    
    print("number of rows:", len(r))
    print("number of cols:", len(r[0]))
    print("num1s before:",num1s)
    
    for x in range (0,len(r)):
        for y in range(1,len(r[0])):
            num1s[y-1] += int(r[x][y])
    
    
    print("num1s after :",num1s)
    
    7
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    4
    import numpy
    
    print ("Reading..")
    txtfile = open("test1.txt", "r")
    print(txtfile.readline())
    txtfile.close()
    
    r= numpy.genfromtxt('test1.txt',dtype=str,skiprows=1)
    
    for x in range (0,len(r)):
        print(r[x])
    
    allTested = [0] * (len(r[0]) - 1)
    num1s = [0] * (len(r[0]) - 1)
    
    print("number of rows:", len(r))
    print("number of cols:", len(r[0]))
    print("num1s before:",num1s)
    
    for x in range (0,len(r)):
        for y in range(1,len(r[0])):
            num1s[y-1] += int(r[x][y])
    
    
    print("num1s after :",num1s)
    
    1
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    0
    Sum of arr :  36.2
    Sum of arr(uint8) :  36
    Sum of arr(float32) :  36.2
    
    Is np.sum(arr).dtype == np.uint :  False
    Is np.sum(arr).dtype == np.float :  True
    1

    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    2
    Sum of arr :  36.2
    Sum of arr(uint8) :  36
    Sum of arr(float32) :  36.2
    
    Is np.sum(arr).dtype == np.uint :  False
    Is np.sum(arr).dtype == np.float :  True
    3
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    4
    Sum of arr :  36.2
    Sum of arr(uint8) :  36
    Sum of arr(float32) :  36.2
    
    Is np.sum(arr).dtype == np.uint :  False
    Is np.sum(arr).dtype == np.float :  True
    5____101010
    Sum of arr :  36.2
    Sum of arr(uint8) :  36
    Sum of arr(float32) :  36.2
    
    Is np.sum(arr).dtype == np.uint :  False
    Is np.sum(arr).dtype == np.float :  True
    8

    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    40
    Sum of arr :  279
    Sum of arr(uint8) :  23
    Sum of arr(float32) :  279.0
    
    Is np.sum(arr).dtype == np.uint :  False
    Is np.sum(arr).dtype == np.float :  False
    0
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    4
    Sum of arr :  36.2
    Sum of arr(uint8) :  36
    Sum of arr(float32) :  36.2
    
    Is np.sum(arr).dtype == np.uint :  False
    Is np.sum(arr).dtype == np.float :  True
    5
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    010
    Sum of arr :  279
    Sum of arr(uint8) :  23
    Sum of arr(float32) :  279.0
    
    Is np.sum(arr).dtype == np.uint :  False
    Is np.sum(arr).dtype == np.float :  False
    5

    Is

    Các

    Output:

    Sum of arr :  36.2
    Sum of arr(uint8) :  36
    Sum of arr(float32) :  36.2
    
    Is np.sum(arr).dtype == np.uint :  False
    Is np.sum(arr).dtype == np.float :  True

    & nbsp; Mã số 3: & NBSP;Code #2: 

    Python3

    import numpy as np

    arr ____10 import8import9

    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    3numpy as np1
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    3numpy as np3
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    3numpy as np5
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    3______8788

    Sum of arr :  36.2
    Sum of arr(uint8) :  36
    Sum of arr(float32) :  36.2
    
    Is np.sum(arr).dtype == np.uint :  False
    Is np.sum(arr).dtype == np.float :  True
    9
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    1arr 1
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    3arr 3
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    3arr 5
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    3arr 7
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    3arr 9
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    00

    Sum of arr :  36.2
    Sum of arr(uint8) :  36
    Sum of arr(float32) :  36.2
    
    Is np.sum(arr).dtype == np.uint :  False
    Is np.sum(arr).dtype == np.float :  True
    9
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    1
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    03
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    3
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    4
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    3
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    07
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    3
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    09
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    3
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    0
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    12

    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    2
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    3
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    4
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    5

    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    2
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    7
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    8
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    9
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    4
    import numpy
    
    print ("Reading..")
    txtfile = open("test1.txt", "r")
    print(txtfile.readline())
    txtfile.close()
    
    r= numpy.genfromtxt('test1.txt',dtype=str,skiprows=1)
    
    for x in range (0,len(r)):
        print(r[x])
    
    allTested = [0] * (len(r[0]) - 1)
    num1s = [0] * (len(r[0]) - 1)
    
    print("number of rows:", len(r))
    print("number of cols:", len(r[0]))
    print("num1s before:",num1s)
    
    for x in range (0,len(r)):
        for y in range(1,len(r[0])):
            num1s[y-1] += int(r[x][y])
    
    
    print("num1s after :",num1s)
    
    1
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    0
    import numpy
    
    print ("Reading..")
    txtfile = open("test1.txt", "r")
    print(txtfile.readline())
    txtfile.close()
    
    r= numpy.genfromtxt('test1.txt',dtype=str,skiprows=1)
    
    for x in range (0,len(r)):
        print(r[x])
    
    allTested = [0] * (len(r[0]) - 1)
    num1s = [0] * (len(r[0]) - 1)
    
    print("number of rows:", len(r))
    print("number of cols:", len(r[0]))
    print("num1s before:",num1s)
    
    for x in range (0,len(r)):
        for y in range(1,len(r[0])):
            num1s[y-1] += int(r[x][y])
    
    
    print("num1s after :",num1s)
    
    3

    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    2
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    7
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    8
    import numpy
    
    print ("Reading..")
    txtfile = open("test1.txt", "r")
    print(txtfile.readline())
    txtfile.close()
    
    r= numpy.genfromtxt('test1.txt',dtype=str,skiprows=1)
    
    for x in range (0,len(r)):
        print(r[x])
    
    allTested = [0] * (len(r[0]) - 1)
    num1s = [0] * (len(r[0]) - 1)
    
    print("number of rows:", len(r))
    print("number of cols:", len(r[0]))
    print("num1s before:",num1s)
    
    for x in range (0,len(r)):
        for y in range(1,len(r[0])):
            num1s[y-1] += int(r[x][y])
    
    
    print("num1s after :",num1s)
    
    7
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    4
    import numpy
    
    print ("Reading..")
    txtfile = open("test1.txt", "r")
    print(txtfile.readline())
    txtfile.close()
    
    r= numpy.genfromtxt('test1.txt',dtype=str,skiprows=1)
    
    for x in range (0,len(r)):
        print(r[x])
    
    allTested = [0] * (len(r[0]) - 1)
    num1s = [0] * (len(r[0]) - 1)
    
    print("number of rows:", len(r))
    print("number of cols:", len(r[0]))
    print("num1s before:",num1s)
    
    for x in range (0,len(r)):
        for y in range(1,len(r[0])):
            num1s[y-1] += int(r[x][y])
    
    
    print("num1s after :",num1s)
    
    1
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    0
    Sum of arr :  36.2
    Sum of arr(uint8) :  36
    Sum of arr(float32) :  36.2
    
    Is np.sum(arr).dtype == np.uint :  False
    Is np.sum(arr).dtype == np.float :  True
    1

    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    2
    Sum of arr :  36.2
    Sum of arr(uint8) :  36
    Sum of arr(float32) :  36.2
    
    Is np.sum(arr).dtype == np.uint :  False
    Is np.sum(arr).dtype == np.float :  True
    3
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    4
    Sum of arr :  36.2
    Sum of arr(uint8) :  36
    Sum of arr(float32) :  36.2
    
    Is np.sum(arr).dtype == np.uint :  False
    Is np.sum(arr).dtype == np.float :  True
    5____101010
    Sum of arr :  36.2
    Sum of arr(uint8) :  36
    Sum of arr(float32) :  36.2
    
    Is np.sum(arr).dtype == np.uint :  False
    Is np.sum(arr).dtype == np.float :  True
    8

    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    40
    Sum of arr :  279
    Sum of arr(uint8) :  23
    Sum of arr(float32) :  279.0
    
    Is np.sum(arr).dtype == np.uint :  False
    Is np.sum(arr).dtype == np.float :  False
    0
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    4
    Sum of arr :  36.2
    Sum of arr(uint8) :  36
    Sum of arr(float32) :  36.2
    
    Is np.sum(arr).dtype == np.uint :  False
    Is np.sum(arr).dtype == np.float :  True
    5
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    010
    Sum of arr :  279
    Sum of arr(uint8) :  23
    Sum of arr(float32) :  279.0
    
    Is np.sum(arr).dtype == np.uint :  False
    Is np.sum(arr).dtype == np.float :  False
    5

    Is

    Các

    Output:

    Sum of arr :  279
    Sum of arr(uint8) :  23
    Sum of arr(float32) :  279.0
    
    Is np.sum(arr).dtype == np.uint :  False
    Is np.sum(arr).dtype == np.float :  False

    & nbsp; Mã số 3: & NBSP;Code #3: 

    Python3

    import numpy as np

    arr ____10 import8import9

    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    3numpy as np1
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    3numpy as np3
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    3numpy as np5
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    3______8788

    Sum of arr :  36.2
    Sum of arr(uint8) :  36
    Sum of arr(float32) :  36.2
    
    Is np.sum(arr).dtype == np.uint :  False
    Is np.sum(arr).dtype == np.float :  True
    9
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    1arr 1
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    3arr 3
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    3arr 5
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    3arr 7
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    3arr 9
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    00

    Sum of arr :  36.2
    Sum of arr(uint8) :  36
    Sum of arr(float32) :  36.2
    
    Is np.sum(arr).dtype == np.uint :  False
    Is np.sum(arr).dtype == np.float :  True
    9
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    1
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    03
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    3
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    4
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    3
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    07
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    3
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    09
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    3
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    0
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    12

    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    2
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    3
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    4
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    5

    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    2
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    7
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    8
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    11
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    0
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    13
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    14__

    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    2
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    7
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    8
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    11
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    0
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    09
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    14__

    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    2
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    33
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    34
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    35
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    36

    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    37
    Sum of arr :  279
    Sum of arr(uint8) :  23
    Sum of arr(float32) :  279.0
    
    Is np.sum(arr).dtype == np.uint :  False
    Is np.sum(arr).dtype == np.float :  False
    0
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    4
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    16____10
    [1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
    
    09
    number of rows: 16
    number of cols: 15
    num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
    
    43__

    Output:

    Sum of arr :  279
    Sum of arr(axis = 0) :  [52 25 93 42 67]
    Sum of arr(axis = 1) :  [120  75  84]
    
    Sum of arr (keepdimension is True): 
     [[120]
     [ 75]
     [ 84]]