running average of items list in python in reverse order -
i need find average of items in 1-dimensional list: example: l = [123,678,234,256,789,-----] first need obtain running average starting last item in list.thus providing resultant list follows: reslist= [416,489.25,426.33,522.5,789---]. plz can 1 suggest simple code in python doing helpful..
here's solution uses recursion:
def runningmean(seq, n=0, total=0): if not seq: return [] total = total+seq[-1] return runningmean(seq[:-1], n=n+1, total=total) + [total/float(n+1)]
demo
print(runningmean([123,678,234,256,789]))
output:
[416.0, 489.25, 426.3333333333333, 522.5, 789.0]
Comments
Post a Comment