I want to sum a 2 dimensional array in python: Here is what I have: def sum1(input):
sum = 0
for row in range (len(input)-1):
for col in range(len(input[0])-1):
sum = sum + input[row][col]
return sum
print sum1([[1, 2],[3, 4],[5, 6]])
It displays 4 instead of 21 (1+2+3+4+5+6 = 21). Where is my mistake? asked May 23, 2012 at 3:43 1
I think this is better: >>> x=[[1, 2],[3, 4],[5, 6]]
>>> sum(sum(x,[]))
21
answered Nov 27, 2012 at 6:07
3 You could rewrite that function as, def sum1(input):
return sum(map(sum, input))
Basically, map(sum, input) will return a list with the sums across all your rows, then, the outer most sum will add up that list. Example: >>> a=[[1,2],[3,4]]
>>> sum(map(sum, a))
10
answered May 23, 2012 at 3:58 machowmachow 1,0121 gold badge9 silver badges16 bronze badges 1 This is yet another alternate Solution In [1]: a=[[1, 2],[3, 4],[5, 6]]
In [2]: sum([sum(i) for i in a])
Out[2]: 21
answered May 14, 2015 at 16:44 AjayAjay 5,0292 gold badges21 silver badges29 bronze badges
0 And numpy solution is just: import numpy as np
x = np.array([[1, 2],[3, 4],[5, 6]])
Result: >>> b=np.sum(x)
print(b)
21
answered May 23, 2012 at 3:50 AkavallAkavall 78.3k47 gold badges198 silver badges244 bronze badges 3 Better still, forget the index counters and just iterate over the items themselves: def sum1(input):
my_sum = 0
for row in input:
my_sum += sum(row)
return my_sum
print sum1([[1, 2],[3, 4],[5, 6]])
One of the nice (and idiomatic) features of Python is letting it do the counting for you. sum() is a built-in and you should not use names of built-ins for your own identifiers. answered
May 23, 2012 at 3:59 mswmsw 41.9k9 gold badges83 silver badges108 bronze badges This is the issue for row in range (len(input)-1):
for col in range(len(input[0])-1):
try for row in range (len(input)):
for col in range(len(input[0])):
Python's
range(x) goes from 0..x-1 already range(...) range([start,] stop[, step]) -> list of integers Return a list containing an arithmetic progression of integers.
range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.
When step is given, it specifies the increment (or decrement).
For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!
These are exactly the valid indices for a list of 4 elements.
answered May 23, 2012 at 3:45 dfbdfb 13k1 gold badge29 silver badges52 bronze badges range() in python excludes the last element. In other words, range(1, 5) is [1, 5) or [1, 4]. So you should just use len(input) to iterate over the rows/columns.
def sum1(input):
sum = 0
for row in range (len(input)):
for col in range(len(input[0])):
sum = sum + input[row][col]
return sum
answered May 23, 2012 at 3:45 spinlokspinlok 3,46316 silver badges25 bronze badges Don't put -1 in range(len(input)-1) instead use: range(len(input))
range automatically returns a list one less than the argument value so no need of explicitly giving -1 answered
May 23, 2012 at 3:46 Kartik AnandKartik Anand 4,3775 gold badges40 silver badges72 bronze badges def sum1(input):
return sum([sum(x) for x in input])
answered
Sep 13, 2018 at 22:49 J F FitchJ F Fitch 1161 silver badge3 bronze badges Quick answer, use... total = sum(map(sum,[array]))
where [array] is your array title.
answered Apr 1, 2018 at 20:54 1 In
Python 3.7 import numpy as np
x = np.array([ [1,2], [3,4] ])
sum(sum(x))
outputs 10
answered Jan 21, 2019 at 14:51 It seems like a general consensus is that numpy is a complicated solution. In comparison to simpler algorithms. But for the sake of the answer being present: import numpy as np
def addarrays(arr):
b = np.sum(arr)
return sum(b)
array_1 = [
[1, 2],
[3, 4],
[5, 6]
]
print(addarrays(array_1))
This appears to be the preferred solution: x=[[1, 2],[3, 4],[5, 6]]
sum(sum(x,[]))
answered Sep 26, 2019 at 0:14
peyopeyo 3393 silver badges14 bronze badges
def sum1(input):
sum = 0
for row in input:
for col in row:
sum += col
return sum
print(sum1([[1, 2],[3, 4],[5, 6]]))
Sefan 7061 gold badge7 silver badges22 bronze badges answered Aug 17, 2021 at 7:57 Speed comparisonimport random
import timeit
import numpy
x = [[random.random() for i in range(100)] for j in range(100)]
xnp = np.array(x)
Methodsprint("Sum python array:")
%timeit sum(map(sum,x))
%timeit sum([sum(i) for i in x])
%timeit sum(sum(x,[]))
%timeit sum([x[i][j] for i in range(100) for j in range(100)])
print("Convert to numpy, then sum:")
%timeit np.sum(np.array(x))
%timeit sum(sum(np.array(x)))
print("Sum numpy array:")
%timeit np.sum(xnp)
%timeit sum(sum(xnp))
ResultsSum python array:
130 µs ± 3.24 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
149 µs ± 4.16 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
3.05 ms ± 44.8 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
2.58 ms ± 107 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
Convert to numpy, then sum:
1.36 ms ± 90.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
1.63 ms ± 26.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
Sum numpy array:
24.6 µs ± 1.95 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
301 µs ± 4.78 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
answered Apr 5 at 10:13 FasmoFasmo 215 bronze badges 1 def sum1(input):
sum = 0
for row in range (len(input)-1):
for col in range(len(input[0])-1):
sum = sum + input[row][col]
return sum
print (sum1([[1, 2],[3, 4],[5, 6]]))
You had a problem with parenthesis at the print command.... This solution will be good now The correct solution in Visual Studio Code McLovin 5796 silver badges19 bronze badges answered Aug 8 at 17:12
1
How do you sum a row in an array in Python?
sum() in Python. The numpy. sum() function is available in the NumPy package of Python. This function is used to compute the sum of all elements, the sum of each row, and the sum of each column of a given array.
How do I sum a row in an array?
Algorithm. Create an array of size equal to the number of rows. This array is used to store the row sum of every row. Let the array be rowSum .. Iterate through every row of the matrix and execute the following: Initialize a sum variable to zero. Loop through all the elements in the row. ... . Return the rowSum array..
How do you sum a column in Python 2D list?
sum(arr, axis, dtype, out) function returns the sum of array elements over the specified axis. To compute the sum of all columns the axis argument should be 0 in sum() function.
How do you find the sum of a row in Python?
To sum all the rows of a DataFrame, use the sum() function and set the axis value as 1. The value axis 1 will add the row values.
|