I have to perform a running average. In the code below, the input file (stress1.txt) contains two columns of x and y values. Every y between 0.9x and 1.1x needs to be averaged. The last part of the code that goes over the two lists is correct, in the sense that it returns the correct x values for each upper and lower bound when I print it out. However, the averaging is not done correctly, and I've tried all possible placements of the mean command. I'm really stuck as what I have seems logically right to me. Also, I'm fairly new to programming so my code might not be very pythonic. Can someone point out what's going wrong?
import sys,string
import numpy as np
from math import *
import fileinput
infiles = ['stress1.txt']
oldlist = []
xlist = []
newlist = [0]
IN = fileinput.input(infiles)
for step in range(21): ## number of rows in stress1.txt
line = IN.readline()
[t,a] = string.split(line)
time = float(t)
acf = float(a)
oldline = [time,acf]
oldlist.append(oldline) ## nested list containing x and y values
for i in range(len(oldlist)):
t11 = float(0.9*oldlist[i][0])
t1 = float("{0:.4f}".format(t11))
t22 = float(1.1*oldlist[i][0])
t2 = float("{0:.4f}".format(t22))
xlist.append(t1)
xlist.append(t2)
xlist = [xlist[i:i+2] for i in range(0, len(xlist), 2)] ## nested list containing upper and lower bounds for each x. This list has the same size as 'oldlist'.
for i in range(len(oldlist)):
for j in range(len(xlist)):
if (xlist[i][0] <= oldlist[j][0] < xlist[i][1]):
#print oldlist[j][0]
newlist.append(oldlist[j][0])
#print '\n'
mean = sum(newlist)/float(len(newlist)) ## not giving the right average
print mean
I have edited my question to include stress1.txt:
0 63.97308696
0.005 62.68978803
0.01 58.95890345
0.015 53.11671683
0.02 45.64732412
0.025 37.10669444
0.03 28.05011931
0.035 18.98414178
0.04 10.34110231
0.045 2.470985737
0.05 -4.356736338
0.055 -9.947472597
0.06 -14.17532845
0.065 -16.97779073
0.07 -18.35134411
0.075 -18.34723586
0.08 -17.0675793
0.085 -14.66065262
0.09 -11.3157742
0.095 -7.257500157
0.1 -2.7383312
The code is expected to average each 'block' as shown below. The initial blocks only contain a single value, so that itself is the average. The later blocks have multiple values which have to be averaged and outputted.{sorry for making this thread so lengthy}
0.005
0.01
0.015
0.02
0.025
0.03
0.035
0.04
0.045
0.045
0.05
0.05
0.055
0.06
0.055
0.06
0.065
0.06
0.065
0.07
0.065
0.07
0.075
0.07
0.075
0.08
0.075
0.08
0.085
0.08
0.085
0.09
0.085
0.09
0.095
0.09
0.095
0.1
0.09
0.095
0.1
newlistto see if it is averaging the right values?