forked from cool-RR/python_toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatistics.py
More file actions
32 lines (22 loc) · 780 Bytes
/
statistics.py
File metadata and controls
32 lines (22 loc) · 780 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Copyright 2009-2017 Ram Rachum.
# This program is distributed under the MIT license.
import numbers
infinity = float('inf')
infinities = (infinity, -infinity)
def get_median(iterable):
'''Get the median of an iterable of numbers.'''
sorted_values = sorted(iterable)
if len(iterable) % 2 == 0:
higher_midpoint = len(iterable) // 2
lower_midpoint = higher_midpoint - 1
return (sorted_values[lower_midpoint] +
sorted_values[higher_midpoint]) / 2
else:
midpoint = len(iterable) // 2
return sorted_values[midpoint]
def get_mean(iterable):
'''Get the mean (average) of an iterable of numbers.'''
sum_ = 0
for i, value in enumerate(iterable):
sum_ += value
return sum_ / (i + 1)