1

Fairly new to python and I have a for loop that resembles this (I won't include the contents since they seem irrelevant):

for i, (A, B) in enumerate(X):
    ...
    arbitrary calculations
    ...
    print s1
    print s2

This cycles through ten times(although it does vary occasionally), giving me 10 values for s1 and 10 for s2. Is there an efficient way of finding the means of these values?

bidby
  • 708
  • 1
  • 10
  • 24
  • We need more information to give you an accurate answer. What arbitrary calculations are being performed? – pzp Mar 31 '16 at 15:38
  • @pzp do we? OP said they're irrelevant. – Adam Smith Mar 31 '16 at 15:39
  • I can update the question to include them if necessary but i feel if the for loop simply iterates through and returns a value for `s1` and `s2` whats done in the middle isn't important? I'm looking for a general answer, hence why i've phrased the question like this – bidby Mar 31 '16 at 15:41
  • 1
    possible duplicate of http://stackoverflow.com/questions/9039961/finding-the-average-of-a-list – mvelay Mar 31 '16 at 15:49

4 Answers4

2

You would need to either append each number to a list, or add them up on the fly before finding the mean.

Using a list:

s1_list = []
s2_list = []

for i, (A, B) in enumerate(X):
    ...
    arbitrary calculations
    ...
    s1_list.append(s1)
    s2_list.append(s2)

s1_mean = sum(s1_list)/float(len(s1_list))
s2_mean = sum(s2_list)/float(len(s2_list))

Adding them up on the fly:

s1_total = 0
s2_total = 0

for i, (A, B) in enumerate(X):
    ...
    arbitrary calculations
    ...
    s1_total += s1
    s2_total += s2

s1_mean = s1_total/float(len(X))
s2_mean = s2_total/float(len(X))

Use float otherwise the mean will be rounded down if it is a decimal number.

Farhan.K
  • 3,425
  • 2
  • 15
  • 26
1

Sure, you can save them to do so.

lst_s1, lst_s2 = [], []

for i, (A,B) in enumerate(X):
    ...
    lst_s1.append(s1)
    lst_s2.append(s2)
    print s1
    print s2

avg_s1 = sum(lst_s1) / len(lst_s1)
avg_s2 = sum(lst_s2) / len(lst_s2)
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
  • 1
    @Kasramvd I don't agree. A "mean" is the sum of a set divided by its length. – Adam Smith Mar 31 '16 at 15:38
  • @Kasramvd Since when is that the definition of the mean? The standard [arithmetic mean](https://en.wikipedia.org/wiki/Mean#Arithmetic_mean_.28AM.29) is the sum of all elements divided by the number of elements. What are you referring to? – zephyr Mar 31 '16 at 15:39
  • 2
    @Kasramvd - you're calculating the _median_, not the mean there – SpoonMeiser Mar 31 '16 at 15:39
  • 1
    @SpoonMeiser that's not even exactly the median, if I remember 6th grade math. The median is `sorted(some_set)[len(some_set) // 2]` – Adam Smith Mar 31 '16 at 15:40
  • and mode is `Counter(some_set).most_common(1)` – Adam Smith Mar 31 '16 at 15:44
  • 1
    @AdamSmith Sorry for confusion. :-P – Mazdak Mar 31 '16 at 15:44
  • 3
    It's odd that you would say "you have to save them to do so". You don't need to save them: you only need to sum them and count them. – khelwood Mar 31 '16 at 15:49
  • @khelwood that's true! You don't even HAVE to count them, since you're enumerating. `s1_sum = float(s1_sum) / i + s1 / i`. Hmm or not -- I'm bad with the math :) – Adam Smith Mar 31 '16 at 15:51
1

I would not allocate lists like in the other answer, just sum inside the loop and divide afterwards by the total number of elements:

sum1 = 0
sum2 = 0
for i, (A, B) in enumerate(X):
    ...
    arbitrary calculations
    ...
    sum1 += s1
    sum2 += s2

n = i+1
print(sum1/n)
print(sum2/n)

Allocation is costly if the lists grow too much bigger.

JulienD
  • 7,102
  • 9
  • 50
  • 84
0

Try following snippet to calculate mean of array. Bottom line is that it will not cause an overflow.

X = [9, 9, 9, 9, 9, 9, 9, 9]

factor = 1000000
xlen = len(X)

mean = (sum([float(x) / factor for x in X]) * factor) / xlen

print(mean)
Saleem
  • 8,728
  • 2
  • 20
  • 34