Summing a 2d array in Python 3.x

D. Leigh

new to programming and Python, using Python 3.x, I have to create a function that adds all the elements in a 2D array, the function should return the addition of all the elements of an array. I have to use 2 for loops to traverse the array and add up all the elements and I can't use any summing functions.

This is what I have so far but it is not functional

def add2D(array):
    for row in array:
        for entry in row:
            print(entry, end='  ')
        print()

    sum = 0
    for row in array (len(input)):
        for col in array(len(input[0])-1):
            sum = sum + input[row][col]
return sum

Can anyone tell me what I'm doing wrong.

Riet

You almost had it. Here's a working version

def add2D(array):
    for row in array:
        for entry in row:
            print(entry, end='  ')
        print()

    sum = 0
    for row in array:
        for col in row:
            sum = sum + col
    return sum

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

python summing over 2d array to get a 1d array

From Dev

MATLAB: Multiplication of 3d array with 2d matrix and summing them by vectorization

From Dev

Summing elements on an array of d3 objects

From Dev

Converting 2d array into 3d array in python

From Dev

Converting 2d array into 3d array in python

From Dev

Summing over first 2D of 3D matrix

From Dev

Insert a 3x3 2D array into a 9x9 2D array

From Dev

3D plot with an 2D array python matplotlib

From Dev

numpy with python: convert 3d array to 2d

From Dev

Add 2d array to make 3d in python

From Dev

Summing elements of numpy array in Python

From Dev

Create a Random Blank Matrix (2D array) in Python 3?

From Dev

Create 2d array from csv file in python 3

From Dev

Need to grab a 3x3 neighbourhood of an input cell from a 2d numpy array

From Dev

How to rotate 3x3 2d array clockwise by n times

From Dev

how to fill 3x3 2d array from numbers 1-9

From Dev

Python/Numpy: rearrange N4xN3xN2xN1 4D array into an (N4.N2)x(N3.N1) 2D array

From Dev

From given row(x),column(y), find 3x3 subarray of a 2D 9x9 array

From Dev

how to create 2d array with 3 elements from 1d array using python

From Dev

Create 3D array from existing 2D array in Python

From Dev

Python: extract a 2D array from a 3D array

From Dev

python from 2D array to 3D coordinates array

From Dev

SSE2: Multiplying signed integers from a 2d array with doubles and summing the results in C

From Dev

SSE2: Multiplying signed integers from a 2d array with doubles and summing the results in C

From Dev

Summing array entries along a particular line, python

From Dev

How to make 1d array multiplied by 2d array resulting in 3d array for python

From Dev

How to make 1d array multiplied by 2d array resulting in 3d array for python

From Dev

numpy.array does not convert my list of lists into 2d numpy array in Python3

From Dev

Copy 2D array to a 3D one - Python / NumPy

Related Related

  1. 1

    python summing over 2d array to get a 1d array

  2. 2

    MATLAB: Multiplication of 3d array with 2d matrix and summing them by vectorization

  3. 3

    Summing elements on an array of d3 objects

  4. 4

    Converting 2d array into 3d array in python

  5. 5

    Converting 2d array into 3d array in python

  6. 6

    Summing over first 2D of 3D matrix

  7. 7

    Insert a 3x3 2D array into a 9x9 2D array

  8. 8

    3D plot with an 2D array python matplotlib

  9. 9

    numpy with python: convert 3d array to 2d

  10. 10

    Add 2d array to make 3d in python

  11. 11

    Summing elements of numpy array in Python

  12. 12

    Create a Random Blank Matrix (2D array) in Python 3?

  13. 13

    Create 2d array from csv file in python 3

  14. 14

    Need to grab a 3x3 neighbourhood of an input cell from a 2d numpy array

  15. 15

    How to rotate 3x3 2d array clockwise by n times

  16. 16

    how to fill 3x3 2d array from numbers 1-9

  17. 17

    Python/Numpy: rearrange N4xN3xN2xN1 4D array into an (N4.N2)x(N3.N1) 2D array

  18. 18

    From given row(x),column(y), find 3x3 subarray of a 2D 9x9 array

  19. 19

    how to create 2d array with 3 elements from 1d array using python

  20. 20

    Create 3D array from existing 2D array in Python

  21. 21

    Python: extract a 2D array from a 3D array

  22. 22

    python from 2D array to 3D coordinates array

  23. 23

    SSE2: Multiplying signed integers from a 2d array with doubles and summing the results in C

  24. 24

    SSE2: Multiplying signed integers from a 2d array with doubles and summing the results in C

  25. 25

    Summing array entries along a particular line, python

  26. 26

    How to make 1d array multiplied by 2d array resulting in 3d array for python

  27. 27

    How to make 1d array multiplied by 2d array resulting in 3d array for python

  28. 28

    numpy.array does not convert my list of lists into 2d numpy array in Python3

  29. 29

    Copy 2D array to a 3D one - Python / NumPy

HotTag

Archive