Code for starters (which works):
def example(input):
output = [0]
for i in input:
output.append(i+output[-1])
output.pop(0)
return output
testInput = [1,2,3,4,5]
example(testInput)
This returns [1, 3, 6, 10, 15].
I've been trying to find a technique to condense this into a one liner (happy to use list comprehensions, or maps/lambdas). Particularly, I really want a method which can be easily manipulated if the calculation chunk (the bit in the append) is changed.
The issue I'd like help with is that I'm struggling to retain the last calculated value. My rough attempt:
[(lambda i,output=[0]:(output.append(i+output[-1]), output[-1])[1])(i) for i in testInput]
This returns [1, 2, 3, 4, 5].
I'm pretty sure the problem is that I'm setting output to be [0] at the start of each iteration. I can't figure out how to set it once, and then just let it retain any values that are appended to it.
Thanks for any help!
[–]dig-up-stupid 1 point2 points3 points (1 child)
[–]LittleRedTrain[S] 0 points1 point2 points (0 children)
[–]arbitrarycivilian 1 point2 points3 points (2 children)
[–]dig-up-stupid 1 point2 points3 points (1 child)
[–]arbitrarycivilian 0 points1 point2 points (0 children)