2

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
6
  • 1
    Have you printed newlist to see if it is averaging the right values? Commented Dec 25, 2015 at 16:00
  • So what is the expected output on this input? Commented Dec 25, 2015 at 16:49
  • Printing oldlist[j][0] (which has been commented out) gives the right values and in the right order, but it isn't averaging it in that order. Commented Dec 25, 2015 at 16:54
  • What is the expected output on this input? Is it? 0.005 0.0075 0.01 0.0125 0.015 0.0175 0.02 0.0225 0.0245454545455 0.0266666666667 0.0284615384615 0.0303571428571 0.0323333333333 0.03375 0.0352941176471 0.0369444444444 0.0381578947368 0.0395 0.0409523809524 0.0420454545455 0.0432608695652 0.0445833333333 0.0456 0.0467307692308 0.047962962963 0.0489285714286 0.05 0.0511666666667 0.0520967741935 0.053125 0.0542424242424 0.0551470588235 0.0561428571429 0.0572222222222 0.0581081081081 0.0590789473684 0.0601282051282 0.060875 0.0617073170732 0.062619047619 Commented Dec 25, 2015 at 17:03
  • 1
    You are not very clear on what you are trying average. What is a 'block' in the above description? Can you reduce the expected input and output to the simplest possible form that will show the problem? Commented Dec 25, 2015 at 17:31

1 Answer 1

3

If I understand your goal correctly, you need an empty newlist[] in the beginning of every iteration. If you do not empty it, it is full of the previous blocks' values.

for i in range(len(oldlist)):

    #Make it empty
    newlist = []

    for j in range(len(xlist)):
        if (xlist[i][0] <= oldlist[j][0] and  oldlist[j][0] <= xlist[i][1]):
            newlist.append(oldlist[j][0])
    print(repr(newlist))
    mean = sum(newlist)/float(len(newlist))
    print(mean)

Produced output:

[0.0]
0.0
[0.005]
0.005
[0.01]
0.01
[0.015]
0.015
[0.02]
0.02
[0.025]
0.025
[0.03]
0.03
[0.035]
0.035
[0.04]
0.04
[0.045]
0.045
[0.045, 0.05, 0.055]
0.04999999999999999
[0.05, 0.055, 0.06]
0.055
[0.055, 0.06, 0.065]
0.06
[0.06, 0.065, 0.07]
0.065
[0.065, 0.07, 0.075]
0.07
[0.07, 0.075, 0.08]
0.07500000000000001
[0.075, 0.08, 0.085]
0.08
[0.08, 0.085, 0.09]
0.085
[0.085, 0.09, 0.095]
0.09000000000000001
[0.09, 0.095, 0.1]
0.09500000000000001
[0.09, 0.095, 0.1]
0.09500000000000001
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Andrea. I was under the impression that the list gets overwritten every time it exits the loop. This makes sense now. Cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.