1

If I have a list of numbers such as [1,2,3,4,5...] and I want to calculate an equation using the first element and first index and add to the next element with index and so on... How can I do that?

my actual list

B = [0.183324722,
     0.240975303,
     0.209108771,
     0.190439972,
     0.142648498,
     0.077993465,
     0.012475611,
     -0.032267127,
     -0.075291522,
     -0.056470670,
     0.076201285,
     0.123893204,
     -0.029201193,
     -0.091173542,
     0.001317696,
     0.026025526]

Here's the code:

RRR = 0.844243567521

for index, Bi in enumerate(B):
    T90Wr = 300 * (np.sum (Bi* ((RRR**(1/6) - 0.65)/0.35)**index))

print T90Wr

Any help will be appreciated

8
  • Hi Alex the problem is not give me the right answer for the equation its supposed to give about 261.1 Commented Apr 2, 2016 at 18:51
  • @Ismail: How sure are you that there isn't an issue with your formula, and what value are you currently getting? Commented Apr 2, 2016 at 18:58
  • Im getting this value 7.8076578 which is completely not right. Commented Apr 2, 2016 at 19:03
  • @Ismail That's because you're not adding anything to T90Wr, just overwriting its value each time in the loop. Also, there's a major error in your formula: 1/6 in Python returns 0, correct it to 1/float(6). Commented Apr 2, 2016 at 19:07
  • first of all, thank you so much for the quick answer. how could i add it as a loop not overwriting. Commented Apr 2, 2016 at 19:12

1 Answer 1

5

I made a small correction to your formula so that 1/6 is evaluated as a float (so that 1/6 == 0.16666666), and not as integer division (which would make 1/6 == 0) in Python 2.7 (in Python 3, 1/6 returns 0.1666666). I suspect this is indeed what you want.

Based on what you said:

I want to calculate an equation using the first element and first index and add to the next element with index and so on...

there are many ways to accomplish this task. I offer three of these methods as pedagogical examples, so that you can write better code in the future.

The Procedural Way

This is the simplest way to do it. Basically, a very simple change to your code can accomplish this task:

T90Wr = 0 # declare an initial value
for index, Bi in enumerate(B):
    T90Wr += 300 * (Bi* ((RRR**(1/float(6)) - 0.65)/0.35)**index)

will work.

The += makes all the difference: it tells Python to add to the existing value of T90Wr, rather than rewrite its current value. The result of this is 257.36.

The Minimalist Way

A second way of doing the same as above but with shorter code:

T90Wr = sum(300 * (Bi* ((RRR**(1/float(6)) - 0.65)/0.35)**index) for index, Bi in enumerate(B))

It uses a list comprehension to create a generator consisting of the results of the formula applied to each value in enumerate(B). Then it makes use of sum() to add them all up. As before, the result is 257.36.

The Functional Way

Finally, you can use reduce in conjunction with enumerate for very powerful and expressive code:

formula = lambda index, Bi : 300*(Bi* ((RRR**(1/float(6)) - 0.65)/0.35)**index)
T90Wr = reduce(lambda total, current: total + formula(*current), enumerate(B), 0)

This will calculate your sum for you, which comes out to 257.36 (reasonably close to what you're looking for, so I assume there's an error in your formula).


If you absolutely want 261.1 as your final answer, I would recommend looking much more closely at the data. When three independent confirmations of the sum have all yielded the same answer, the only error must lie either in the data or in the formula.

Sign up to request clarification or add additional context in comments.

Comments

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.