bpo-37158: Simplify and speed-up statistics.fmean() (GH-13832)

This commit is contained in:
Raymond Hettinger 2019-06-05 07:39:38 -07:00 committed by GitHub
parent ccf0efbb21
commit 6c01ebcc0d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 4 deletions

View file

@ -320,11 +320,11 @@ def fmean(data):
except TypeError: except TypeError:
# Handle iterators that do not define __len__(). # Handle iterators that do not define __len__().
n = 0 n = 0
def count(x): def count(iterable):
nonlocal n nonlocal n
n += 1 for n, x in enumerate(iterable, start=1):
return x yield x
total = fsum(map(count, data)) total = fsum(count(data))
else: else:
total = fsum(data) total = fsum(data)
try: try:

View file

@ -0,0 +1 @@
Speed-up statistics.fmean() by switching from a function to a generator.