Sum of array in python - Stack Overflow
How to cumulative sum over for loop?
python - Get sum of numbers using a Function and For Loop - Stack Overflow
Summing up elements in a list in python using a for loop - Stack Overflow
Videos
Hello, so I am trying to understand how to sum values through a for loop within python so that I start with a value, and then add to it, and then add the next value to that, and then add the next value to that, and so on, going through all values in my list/folder. I think this would be referred to as a "cumulative sum". So let's say I have a list of numbers:
list = [1,4,6,3,7,5,2,8,9]
I want to iterate over each value in this list and add all values to the previous like so:
for value in list:
1 + value
sum = 1 + value
print(sum)or something like that? I tried this, and it just adds one to each value in the list, when I want a single final cumulative sum produced. I am really confused about if my logic is correct there. Is this how I would carry out this cumulative sum operation? I am trying to take 1, and then add 4, and then add 6 to that, and then add 3 to that sum, and then add 7 to that sum, and so on. I realize one could also just add all of these values at once, but I am trying to understand how to do this with a for loop. Thanks!
By for loop
def sumAll(n):
sum_all = 0
for i in range(1, n+1):
sum_all = sum_all + i
return sum_all
number = int(raw_input("Please enter a number: \n"))
print ("The answer is: ") + str(sumAll(number))
Output:
Please enter a number:
10
The answer is: 55
You can also use list Comprehension:
print sum([i for i in range(number+1)])
Output:
55
You can also use a mathematical series formula:
def sumAll(n):
return n * (n + 1) / 2
you can even do it without a loop:
def sumAll(n):
return sum(range(1,n+1))
print(sumAll(10)) # -> 55
if you insist on using a loop:
def sumAll(n):
s = 0
for i in range(1,n+1):
s += i
return s
but the mathematical solution is simply:
def sumAll(n):
return n * (n + 1) / 2
# in python 3:
# return n * (n + 1) // 2
(there are n elements with an average value of (n+1)/2; this will always be an integer since either n or n+1 is even; the proof by induction is often the first example when math students learn about induction...).
>>> phonelist = [[12,14,16,17,18],[16,23,54,64,32]]
>>> [sum(li) for li in phonelist]
[77, 189]
>>> sum([sum(li) for li in phonelist])
266
or:
>>> sum(sum(li) for li in phonelist) # generator expression...
266
If you are trying to create individual categories, you can use a dict:
data={'Bar {}'.format(i):sum(li) for i, li in enumerate(phonelist, 1)}
data['Total']=sum(data.values())
print data
{'Bar 2': 189, 'Bar 1': 77, 'Total': 266}
Then if you want to produce a simple bar graph:
for bar in sorted(data.keys()):
print '{}: {}'.format(bar, int(round(25.0*data[bar]/data['Total']))*'*')
Prints:
Bar 1: *******
Bar 2: ******************
Total: *************************
len(phonelist[0]) is an int, so you can't loop over it. You can change that to
for x in phonelist[0]:
This way x will take on each value of phonelist[0].