Last active
November 30, 2019 12:21
-
-
Save SteadBytes/ae934f51eb27229d5412a65f14ce344a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import count | |
from functools import reduce | |
def iter_mean(iterable): | |
""" | |
Caluculate the arithmetic mean of an iterable *without* loading the entire collection | |
into memory or knowing the legnth apriori. | |
""" | |
return reduce( | |
lambda c, i: (c * (i[1] - 1) + i[0]) / i[1], zip(iterable, count(1)), 0 | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
IPython
%timeit
examples: