-4
lx = [92770.0, 90204.9, 89437.3, 88868.4, 88298.8, 87505.0, 86792.7, 85938.7, 84652.2, 82803.4, 80174.3, 76559.3, 71584.5]
Lx = [] 

I want Lx to be a list containing the average of every two adjacent numbers in lx. eg Lx = [(92770.0 + 90204.9)/2, (90204.+89437.3)/2, etc..... Please help

Muchendu
  • 1
  • 2
  • 2
    What should the last number be the average with? What have you tried? – Sayse Jul 28 '21 at 17:46
  • `[(lx[i] + lx[i+1])/2 for i in range(len(lx)) if i != len(lx)-1]` Heads up - the question shows lack of research, so you're probably going to get a lot of downvotes which would eventually lead to your question being closed. https://stackoverflow.com/help/minimal-reproducible-example – samkart Jul 28 '21 at 17:56
  • 2
    @samkart Wouldn't it be easier to just do `for i in range(len(lx)-1)`? – Tomerikoo Jul 28 '21 at 17:58
  • @Tomerikoo that's right! – samkart Jul 28 '21 at 17:58
  • Note that this is already in one of the answers @samkart . It is better to avoid "answers" in comments because we can't vote on them... – Tomerikoo Jul 28 '21 at 17:59
  • @Tomerikoo that's alright. I wouldn't have answered it because my comment is just a variation of an answer. – samkart Jul 28 '21 at 18:01
  • This is called running mean or moving average, you can find many solutions for python. Eg here https://stackoverflow.com/a/27681394/5239109 – ffsedd Jul 28 '21 at 18:41

4 Answers4

1
lx = [92770.0, 90204.9, 89437.3, 88868.4, 88298.8, 87505.0, 86792.7, 85938.7, 84652.2, 82803.4, 80174.3, 76559.3, 71584.5]

Lx = [sum(x) / 2 for x in zip(lx, lx[1:])]
print(Lx)

Prints:

[91487.45, 89821.1, 89152.85, 88583.6, 87901.9, 87148.85, 86365.7, 85295.45, 83727.79999999999, 81488.85, 78366.8, 74071.9]
Алексей Р
  • 7,507
  • 2
  • 7
  • 18
0

Here is the solution

lx = [92770.0, 90204.9, 89437.3, 88868.4, 88298.8, 87505.0, 86792.7, 85938.7, 84652.2, 82803.4, 80174.3, 76559.3, 71584.5]
Lx = [] 

for i in range(len(lx)-1):
    Lx.append((lx[i]+lx[i+1])/2)
print(Lx)
Lakshika Parihar
  • 1,017
  • 9
  • 19
0

A solution using numpy broadcasting would be:

result = (np.array(lx[:-1])+np.array(lx[1:]))/2
econbernardo
  • 98
  • 1
  • 7
0

How about this ? sorry for noob way

for i in range(len(lx)-1):
    Lx.append((lx[i] + lx[i+1]) / 2)
Bongcoy
  • 33
  • 3