python - Cumulative Sum of a list of numbers -
this question has answer here:
- how find cumulative sum of numbers in list? 15 answers
i have checked other questions similar none of them seem answer problem. need write function returns cumulative sum of list of number. have doesn't seem work properly:
numbers = [4,3,6] sums = [] def cumulativesum(numbers): in range(len(numbers) - 1): sums.append(numbers[i] + numbers[i + 1]) return sums print cumulativesum(numbers) ##[4, 7, 13] answers should come out
this work, never accessing sum array, need in order increment values calculated , put
numbers = [4,3,6] sums = [] def cumulativesum(numbers): sums.append(numbers[0]) in range(len(numbers) - 1): if == 0: sums.append(numbers[i] + numbers[i + 1]) else: sums.append(numbers[i + 1] + sums[i]) return sums print cumulativesum(numbers)
Comments
Post a Comment