1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2013
Location
Chicago
Posts
17
Rep Power
0

#### Array calculations

Hi,
i am in a little bit of a confusion here... i have an array of FLOATS (about 7000 entries), all of them are floats, and i need to make an average of the entries, if i am not mistaken, the math for finding the average is :

average = (element + next element ) / Total of elements
so in my case, i need to
average = (element [0] + element [1] + element [3] etc... ) / len(array)

i was looking at the manual for python and the list comprehension seems to be way to go, but i don't understand how to setup the expression to make it work, the example in the book (Mapping Lists )
[elem*2 for elem in li]
would produce the result, where EACH element of the array is multiplied by 2, and this is not what i need,
could some one point me in a direction?

thanks !
2. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2013
Location
Chicago
Posts
17
Rep Power
0
Oh, ok, looks like i wasn't patient enough and jumped the gun, read about the SUM() function in python, does the job

average = sum(array)/len(array)

Simple enough
3. Use sum. If you needed some other sort of accumulation, use functools.reduce .

Code:
```>>> import functools
>>> import operator